简体   繁体   English

从 Firebase firestore 查询数据时如何在 stream 内部等待

[英]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.

相关问题 查询 Firebase Firestore 数据 - Querying Firebase Firestore Data 从 Firestore 查询数据作为可观察的 - Querying data from Firestore as observable 在流生成器中从 firebase firestore 集合中读取数据 - reading data from firebase firestore collection at stream builder React function 不等待来自 Firestore 的数据 - React function does not await data from Firestore 如何在 firebase get 请求中等待 promise - How to await a promise inside a firebase get request Firebase Firestore 的 snapshots() 是如何制作的,以及它如何使用 flutter 应用程序中的 stream 监听数据库更新 - How the Firebase Firestore's snapshots() is made and how it can listen to the database updates with a stream inside the flutter app SwiftUI 和 MVVM:如何在 View 中显示来自 Firebase/Firestore 的数据? - SwiftUI and MVVM: How to display data from Firebase/Firestore in View? 如何从 firebase firestore 获取数据并将数据存储在 useState 钩子中 - How to fetch data from firebase firestore and store the data in useState hook 如何获取 stream 的 firestore 数据? - How to get firestore data as stream? 如何将数据从 Firebase Firestore 模拟器导出到实际的 Firebase Firestore 数据库 - How to export data from Firebase Firestore emulator to actual Firebase Firestore database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM