简体   繁体   中英

i got this error when i am trying to use snapshot.data.docs.length in listview.buider: The getter 'docs' isn't defined for the type 'Object'

I am trying to fetch data from firebase to list all the documents in a listview builder the code is still not completed in term of displaying the database filed in the code. this is the error: The getter 'docs' isn't defined for the type 'Object'

Container(
              child: StreamBuilder<Object>(
                  stream: _firestore
                      .collection('Patient')
                      .doc(_auth.currentUser.email)
                      .collection("Diabetes")
                      .snapshots(),
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      return ListView.builder(
                          reverse: true,
                          shrinkWrap: true,
                          itemCount:
                              snapshot.data.docs.length, // here is the error "docs"
                          itemBuilder: (context, index) {
                            DocumentSnapshot documentSnapshot =
                                snapshot.data.docs[index]; // also another error "docs"
                            return Container();
                          });
                    }
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  }),
            )

You should replace snapshot.data.docs.length with snapshot.data.length

 Container(
    child: StreamBuilder<Object>(
       stream: _firestore
         .collection('Patient')
         .doc(_auth.currentUser.email)
         .collection("Diabetes")
         .snapshots(),
       builder: (context, snapshot) {
         if (snapshot.hasData) {
           return ListView.builder(
               reverse: true,
               shrinkWrap: true,
               itemCount: snapshot.data.length,
               itemBuilder: (context, index) {
                 DocumentSnapshot documentSnapshot = snapshot.data[index];
                 return Container();
               });
        }
        return Center(
         child: CircularProgressIndicator(),
        );
     }),
    )

I solved the problem by replacing StreamBuilder<Object> with StreamBuilder<QuerySnapshot> . by default the StreamBuilder comes in this form StreamBuilder<Object>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM