我可以在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;
});
});
这有效,但我不想返回一个流,因为我只想检索一次数据以迭代列表并对其执行操作。
使用getDocuments()在Query或CollectionReference上查询文档一次。
QuerySnapshot querySnapshot = await Firestore.instance.collection("collection").getDocuments();
var list = querySnapshot.documents;