user8103839
user8103839

Reputation:

Flutter / Google Firestore StreamBuilder not upToDate with Database

I have a problem with my StreamBuilder. In this case I have a TabBarView with 3 StreamBuilders (code down this post with one of the three tabs). When something is deleted from the db, the builder shows wrong data. Sometimes the item is still there, sometimes it doubles. I tried to delete the two orderBy statements from the query. I hot reloaded and the data was right. After adding the statements again and hot reloaded, everything was fine.

Why is this happening? Is it because of the orderBy statements?

Thanks!

StreamBuilder(
          stream: Firestore.instance
              .collection("bookings")
              .document(localUser.centerName)
              .collection("center_bookings")
              .where("customerId", isEqualTo: localUser.userId)
              .orderBy("bookedMonth", descending: false)
              .orderBy("bookedDay", descending: false)
              .snapshots(),
          builder: (BuildContext context,
              AsyncSnapshot<QuerySnapshot> snapshot) {
            if (snapshot.hasError) {
              return Text("Error: ${snapshot.error}");
            }
            switch (snapshot.connectionState) {
              case ConnectionState.waiting:
                return Center(
                  child: CircularProgressIndicator(
                    backgroundColor: Theme.of(context).primaryColor,
                  ),
                );
              default:
                return Container(
                  margin: const EdgeInsets.symmetric(
                      horizontal: 5.0, vertical: 10.0),
                  child: ListView(
                      children: snapshot.data.documents.map((document) {
                    if (_datesNext7Days.contains(document["bookedFor"])) {
                      return Padding(...);
                    } else {
                      return SizedBox();
                    }
                  }).toList()),
                );
            }
          },
        ),

Upvotes: 0

Views: 35

Answers (1)

sourav pandit
sourav pandit

Reputation: 9115

try to return yeild yourdata type for continues stream of data in streambuilder here is the example and don't forget the * in async

 static Stream<List<MeetingItem>> getMeetingsList() async* {
   Firestore fireStore = Firestore.instance;
   try {
    var collectionsMeeting = fireStore.collection(VConstants.KEYS.meetings);

    var snapshots = collectionsMeeting.snapshots();

   await for (QuerySnapshot snap in snapshots) {
    List<MeetingItem> meetingList = [];
    snap.documents.forEach((element) {
      meetingList.add(MeetingItem(element.documentID, element.data));
    });
    yield meetingList;
  }
 } catch (e) {
  print(e);
 }
}
}

call this method from this line

 StreamBuilder(
      stream: getMeetingsList()//customize it with your return value

Upvotes: 1

Related Questions