【发布时间】:2019-09-03 13:00:18
【问题描述】:
我可以在 firestore 中添加和更新数据,我还可以检索集合流并将其转换为对象列表,但我不能只检索一次集合并将其转换为对象列表。
// 从 Firestore 获取流
Stream<QuerySnapshot> getDataDateStream(String uid, int startDateTime, int endDateTime) {
CollectionReference usersDataCollection = Firestore.instance.collection('users').document(uid).collection('data');
Stream<QuerySnapshot> snapshots = dataCollection.where('dataDateTime', isGreaterThanOrEqualTo: startDateTime).where('dataDateTime', isLessThanOrEqualTo: endDateTime).snapshots();
return snapshots;
}
// 将 Stream 转换为列表
List<DataSavedModel> ListToday = List<DataSavedModel>();
StreamSubscription<QuerySnapshot> dataSubToday;
dataSubToday = db.getDataDateStream(appState.user.uid, startTimeToday, todayEndTime).listen((QuerySnapshot snapshot) {
final List<DataSavedModel> ModelListToday = snapshot.documents.map((documentSnapshot) => DataSavedModel.fromMap(documentSnapshot.data)).toList();
setState(() {
this.ListToday = ModelListToday;
});
});
这可行,但我不想返回流,因为我只想检索一次数据以迭代列表并对其执行操作。
【问题讨论】:
标签: dart flutter google-cloud-firestore