Flutter / Google Firestore StreamBuilder不与数据库保持最新

时间:2020-02-28 11:55:16

标签: flutter google-cloud-firestore

我的StreamBuilder出现问题。 在这种情况下,我有一个带有3个StreamBuilders的TabBarView(用三个选项卡之一来编码此帖子)。从数据库中删除某些内容后,构建器将显示错误的数据。有时物品仍然在那里,有时会翻倍。我试图从查询中删除两个ord​​erBy语句。我热装,数据正确。再次添加语句并热加载后,一切都很好。

为什么会这样?是因为orderBy语句吗?

谢谢!

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()),
                );
            }
          },
        ),

1 个答案:

答案 0 :(得分:1)

尝试返回yourd数据类型以在streambuilder中继续数据流 这是示例,不要忘记异步中的*

 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);
 }
}
}

从此行调用此方法

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