skip to Main Content

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,
                  ),
                );
              });
        });
  }
}

2

Answers


  1. 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,
                      ),
                    );
                  });
            });
      }
    }
    
    Login or Signup to reply.
  2. 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();
            });
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search