'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

  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. I can't await inside the map function or the return type of Stream<List> changes. so how can i await my function call here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source