Home > Mobile >  Getting Null Value while Using Stream in Flutter
Getting Null Value while Using Stream in Flutter

Time:07-20

I was to trying to get an images from my firestore database. I used streambuilder to get images but it is showing me an error null value received. I checked the code completely and it was perfect. I dont know where the problem exist. Kindly help. Thanks.

Error Null check operator used on a null value. The following _CastError was thrown building StreamBuilder<QuerySnapshot<Map<String, dynamic>>>(dirty, state: _StreamBuilderBaseState<QuerySnapshot<Map<String, dynamic>>, AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>>>#b7a15):

GetImage Code

class GetImage extends StatelessWidget {
  const GetImage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('users')
            .doc(FirebaseAuth.instance.currentUser!.uid)
            .collection('cart')
            .snapshots(),
        builder: (context,
            AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
          
          return ListView.builder(
              itemCount: snapshot.data!.docs.length,
              itemBuilder: (context, index) {
                return Container(
                  child: Image.network(
                    snapshot.data!.docs[index].data()['imageUrl'],
                    fit: BoxFit.cover,
                    alignment: Alignment.center,
                  ),
                );
              });
        });
  }
}

CodePudding user response:

The error is showing because you have not provided the code for the loading state of a streambuilder, when the data is being loaded. Here is the updated code.

class GetImage extends StatelessWidget {
  const GetImage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('users')
            .doc(FirebaseAuth.instance.currentUser!.uid)
            .collection('cart')
            .snapshots(),
        builder: (context,
            AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(
              child: CircularProgressIndicator(), // or the data you want to show while loading...
            );
          }

          return ListView.builder(
              itemCount: snapshot.data!.docs.length,
              itemBuilder: (context, index) {
                return Container(
                  child: Image.network(
                    snapshot.data!.docs[index].data()['imageUrl'],
                    fit: BoxFit.cover,
                    alignment: Alignment.center,
                  ),
                );
              });
        });
  }
}

CodePudding user response:

You need to wait to fetch the data

class GetImage extends StatelessWidget {
  const GetImage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('users')
            .doc(FirebaseAuth.instance.currentUser!.uid)
            .collection('cart')
            .snapshots(),
        builder: (context,
            AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot) {
          if (snapshot.hasData) {
            return ListView.builder(
                itemCount: snapshot.data!.docs.length,
                itemBuilder: (context, index) {
                  return Container(
                    child: Image.network(
                      snapshot.data!.docs[index].data()['imageUrl'],
                      fit: BoxFit.cover,
                      alignment: Alignment.center,
                    ),
                  );
                });
          }
          if (snapshot.hasError) {
            return Text("got error ${snapshot.error}");
          }
          return CircularProgressIndicator();
        });
  }
}
  • Related