0

this is the error msg i get from the consol

======== Exception caught by widgets library ======================================================= The following _CastError was thrown building StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(dirty, state: _StreamBuilderBaseState<DocumentSnapshot<Map<String, dynamic>>, AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>>>#3062d): Null check operator used on a null value

class _LandingScreenState extends State<LandingScreen> {


  final Stream<DocumentSnapshot<Map<String, dynamic>>> _vendor =
      FirebaseFirestore.instance.collection('vendor').doc(FirebaseAuth.instance.currentUser!.uid).snapshots();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
        stream: _vendor,
        builder:
            (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
          if (snapshot.hasError) {
            return const Center(child: Text('Something went wrong'));
          }

          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(child: CircularProgressIndicator());
          }

          if (!snapshot.data!.exists) {
            return const RegistrationScreen();
          }

          Vendor vendor =
              Vendor.fromJson(snapshot.data!.data() as Map<String, dynamic>);
          if (vendor.approved == true) {
            return const HomeScreen();
          }

          return Scaffold(
            body: Center(
                child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  height: 80,
                  width: 80,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(20),
                    child: CachedNetworkImage(
                      imageUrl: vendor.logo!,
                      placeholder: (context, url) => Container(
                        height: 80,
                        width: 80,
                        color: Colors.grey,
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: Container(
                    decoration: const BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.all(Radius.circular(20)),
                    ),
                    child: const Padding(
                      padding: EdgeInsets.all(20.0),
                      child: Text(
                        'Your application received!\nAdmin will contact you for a confirmation!',
                        textAlign: TextAlign.start,
                        style: TextStyle(
                            fontSize: 25, fontWeight: FontWeight.normal),
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(15.0),
                  child: Container(
                    decoration: const BoxDecoration(
                      color: Colors.teal,
                      borderRadius: BorderRadius.all(Radius.circular(50)),
                    ),
                    child: const Padding(
                      padding: EdgeInsets.all(20.0),
                      child: Text(
                        'blah blah',
                        textAlign: TextAlign.start,
                        style: TextStyle(
                            fontSize: 15, fontWeight: FontWeight.normal),
                      ),
                    ),
                  ),
                ),
                TextButton(
                  child: const Text('Sign Out'),
                  onPressed: () {
                    FirebaseAuth.instance.signOut().then((value) {
                      Navigator.of(context).pushReplacement(
                        MaterialPageRoute(
                          builder: (BuildContext context) => LoginScreen(),
                        ),
                      );
                    });
                  },
                )
              ],
            )),
          );
        },
      ),
    );
  }
}

REX2420
  • 1
  • 1

1 Answers1

0

It is most likely because you did exactly what the error says: used a null check operator on a null value. You have several potential places it could be occurring, although I'm guessing that the rest of the error message gives you the exact line.

First you have several things assuming that there is data when you haven't checked, for example !snapshot.data!.exists or Vendor.fromJson(snapshot.data!.data() Second, you use a null check operator on this when you also haven't checked to see if logo exists either vendor.logo!.

First check to see if those things are null, eg vendor.logo != null ? vendor.logo : '' as one example

Jason Simpson
  • 4,653
  • 5
  • 16
  • 26