我正在尝试使用 Flutter 的 StreamBuilder 在我的 Flutter 应用程序上显示 Google-Firestore 实时列表。
但是,流似乎同时使用了旧数据和新数据,而不仅仅是新数据。 (见下文)
//This is the widget with the stream
Expanded(
child: StreamBuilder<QuerySnapshot>(
stream: _firestoreInstance.collection("seeks").document('145725').collection("classes").document(_chosenClass)
.collection("classTopics").document(date).collection("dateTopics").snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
if (!snapshot.hasData) return Text('Loading...');
return ListView(
children: snapshot.data.documents.map((DocumentSnapshot document){
debugPrint("DOCCUMENT ID: ${document.documentID} AND topic Name ${document['topicName']}");
return ListTile(
title: Text(document['topicName']),
subtitle: TopicSlider(_chosenClass, document.documentID), //pass class name and topic id to slider to save in firebase
);
}).toList(),
);
},
),
)
我选择 CS115 和日期,这让我得到了正确的列表
文档 ID 也正确(debugPrint 的输出):
但是,一旦我把班级改为CS331
这发生了。
.
如您所见,CS331 的前三个文档 ID 与 CS115 的 ID 完全相同。似乎流在使用新数据之前只使用了之前存在的任何数据。我不明白为什么会这样,因为每次更改 dropDownItem 时我都会调用 —— 这样整个小部件就可以重建(从而用更新的数据重新生成一个新的流对象)。这会导致新 UI 使用旧数据的值。我已经在这个问题上停留了几天了,似乎没有任何效果。有谁知道如何解决这个奇怪的错误?提前致谢!