简体   繁体   中英

Firestore query returns only one document

I have an app where admin can delete all documents in the firebase collection and add an x number of new documents, this works beautifully, but my streambuilder isn't updating properly, the stream builder is getting back only one document everytime you delete all documents and create new ones, it only returns one, and like when you leave the app and come back, it fetches the proper amount of documents, all I can find online is that it's wrong to use a loop when querying and I've removed my for loop and am now using the map method, still, it is the same, here is my stream builder code

StreamBuilder<QuerySnapshot>(
          stream: _store.collection("picks").snapshots(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              List<PickCard> pickCards = [];
              final documentSnapshots = snapshot.data!.docs;
              debugPrint(documentSnapshots.length.toString());
              if (documentSnapshots.isNotEmpty) {
                documentSnapshots.map((e) {
                  pickCards.add(
                    PickCard(
                    pickerPosition: e["pickerPosition"],
                    pickerName: e["pickerName"],
                    isPicked: e["isPicked"],
                    pickerEmail: e["pickerEmail"],
                  ),);
                }).toList();
                dHelp.setCards(
                  context,
                  pickCards,
                );
                dHelp.setContributors(context, documentSnapshots.length);
              }
            } else {
            }

the print document snapshot length is always 1 when they get created, but after refresh, the actual length updates, but in the firebase console, everything works perfectly, the documents update effectively,

here is a video of the problem https://www.dropbox.com/s/25qqnh0ttgemgf1/2022-08-16%2010-26-46.mp4?dl=0

I found that passing the stream directly to the streamBuilder was causing the stream to restart each time the build method rebuilt, which was supposed to be whenever the stream returns new data, so, it was kinda knotting over itself, I instantiated the stream in the state then passed it to the streamBuilder, so now it's only created once in the lifetime of the page

// created this variable
late Stream<QuerySnapshot> _stream;

@override
initState() {
// gave it a value in iniState
 _stream = _store.collection("picks").snapshots();
 super.initState();
}

StreamBuilder<QuerySnapshot>(
          stream: _stream, // then added this here
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              List<PickCard> pickCards = [];
              final documentSnapshots = snapshot.data!.docs;
              debugPrint(documentSnapshots.length.toString());
              if (documentSnapshots.isNotEmpty) {
                documentSnapshots.map((e) {
                  pickCards.add(
                    PickCard(
                    pickerPosition: e["pickerPosition"],
                    pickerName: e["pickerName"],
                    isPicked: e["isPicked"],
                    pickerEmail: e["pickerEmail"],
                  ),);
                }).toList();
                dHelp.setCards(
                  context,
                  pickCards,
                );
                dHelp.setContributors(context, documentSnapshots.length);
              }
            } else {
            }

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