Combine streams from Firestore in flutter. The problem is not in the merging, but in the StreamBuilder updating the UI based on the LATEST snapshot, in other words it doesn’t stack snapshots it just picks up that last emitted an event, in other words the streams are merged and the merged stream does contain the data of all merged streams, however the streamBuilder will only show the very Last stream emitted event, a work around is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | StreamBuilder<List<QuerySnapshot>>(stream: streamGroup, builder: (BuildContext context, AsyncSnapshot<List<QuerySnapshot>> snapshotList){ if(!snapshotList.hasData){ return MyLoadingWidget(); } // note that snapshotList.data is the actual list of querysnapshots, snapshotList alone is just an AsyncSnapshot int lengthOfDocs=0; int querySnapShotCounter = 0; snapshotList.data.forEach((snap){lengthOfDocs = lengthOfDocs + snap.documents.length;}); int counter = 0; return ListView.builder( itemCount: lengthOfDocs, itemBuilder: (_,int index){ try{DocumentSnapshot doc = snapshotList.data[querySnapShotCounter].documents[counter]; counter = counter + 1 ; return new Container(child: Text(doc.data["name"])); } catch(RangeError){ querySnapShotCounter = querySnapShotCounter+1; counter = 0; DocumentSnapshot doc = snapshotList.data[querySnapShotCounter].documents[counter]; counter = counter + 1 ; return new Container(child: Text(doc.data["name"])); } }, ); }, |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.