[英]How to await inside a stream while querying data from Firebase firestore
For context I'm using Getx state management for flutter and i need to call list.bindStream(availabilityStream())
on my Rx<List<Availability>>
object. here is my availabilityStream method对于上下文,我正在使用 Getx state 管理 flutter,我需要在我的Rx<List<Availability>>
object 上调用list.bindStream(availabilityStream())
。这是我的 availabilityStream 方法
static Stream<List<Availability>> availabilityStream() {
return FirebaseFirestore.instance
.collection('availability')
.where('language',
isEqualTo: GetStorageController.instance.language.value)
.snapshots()
.map((QuerySnapshot query) {
List<Availability> results = [];
for (var availablity in query.docs) {
availablity["cluster"].get().then((DocumentSnapshot document) {
if (document.exists) {
print("Just reached here!");
//! Ignore doc if cluster link is broken
final model = Availability.fromDocumentSnapshot(
availabilityData: availablity, clusterData: document);
results.add(model);
}
});
}
print("result returned");
return results;
});
}
the cluster field on my availability collection is a reference field to another collection.我的可用性集合上的集群字段是另一个集合的引用字段。 The problem here is i need to await the.get() call to my firestore or the function returns before the data gets returned.这里的问题是我需要等待 .get() 调用我的 firestore 或 function 在返回数据之前返回。 I can't await inside the map function or the return type of Stream<List> changes.我无法在 map function 或 Stream<List> 的返回类型更改中等待。 so how can i await my function call here?那么我如何才能在这里等待我的 function 电话呢?
using the advice i got from the comments I've used Stream.asyncMap to wait for all my.network call futures to complete.使用我从评论中得到的建议,我使用 Stream.asyncMap 等待所有 my.network 调用期货完成。
Here is my updated Repository这是我更新的存储库
class AvailabilityRepository {
static Future<Availability> getAvailabilityAndCluster(
QueryDocumentSnapshot availability) async {
return await availability["cluster"]
.get()
.then((DocumentSnapshot document) {
if (document.exists) {
//! Ignore doc if cluster link is broken
final model = Availability.fromDocumentSnapshot(
availabilityData: availability, clusterData: document);
return model;
}
});
}
static Stream<List<Availability>> availabilityStream() {
return FirebaseFirestore.instance
.collection('availability')
.where('language',
isEqualTo: GetStorageController.instance.language.value)
.snapshots()
.asyncMap((snapshot) => Future.wait(
snapshot.docs.map((e) => getAvailabilityAndCluster(e))));
}
}
How i think this works is that the normal.map function returns multiple promises form the getAvailabilityAndCluster() method then all of the processes that execute asynchronously are all put to Future.wait() which is one big promise that waits all the promises inside it to complete.我认为这是如何工作的是 normal.map function 从 getAvailabilityAndCluster() 方法返回多个承诺然后所有异步执行的进程都被放入 Future.wait() 这是一个大的 promise 等待它里面的所有承诺去完成。 Then this is passed onto.asyncMap() which waits for the Future.wait() to complete before continuing with its result.然后将其传递到 asyncMap() 上,等待 Future.wait() 完成,然后再继续其结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.