简体   繁体   中英

Unable to add all Firestore documents to a local variable in Flutter

I have a Firestore collection called cattle-feed . It has 7 documents. I am using a StreamBuilder to fetch these documents via a snapshot and add them to a list called products . products is initialized as an empty list.

Issue faced:

I am only able to add the first 4 documents of the cattle-feed collection to the list variable products .

My Code:

StreamBuilder(
      stream: FirebaseFirestore.instance.collection('cattle-feed').snapshots(),
         builder: (context, snapshot) {
           if(snapshot.hasData){
              products.clear();
                return ListView.builder(
                    scrollDirection: Axis.horizontal,
                        itemCount: snapshot.data!.docs.length,
                        itemBuilder: (context, index2){
                          // products.clear();
                          products.add(Map.from(snapshot.data!.docs[index2].data()));

What is wrong with my code which is letting me copy only 4 Firestore documents instead of all 7 and what is the solution?

try replace:

  products.add(Map.from(snapshot.data!.docs[index2].data()));

with:

  products.add(snapshot.data!.docs[index2].data() as Map<String, dynamic>);

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