devmer
devmer

Reputation: 63

Flutter Get Firestore Documents stream by list of IDs

I am looking for a way to get a stream of documents by another stream of Ids, Let's say I have a stream of document Ids like so Stream<List<String>> docIds = ['3dd34334, '45454de', ...] then I need to query another collection using the ids I already have please find my code below which returns an empty stream.


Stream<List<ViewModel>> matches(uid) async { 

  Stream<List<String>> docIds = FirebaseFirestore.instance
      .collection('matches')
      .where('match', arrayContains: uid)
      .snapshots()
      .map((event) => event.docs.map((e) => e.id).toList());

  return docIds.map((e) {
    e.map((e) => FirebaseFirestore.instance
        .collection('matches')
        .doc(e)
        .collection("myCollection")
        .snapshots()
        .map(
          (event) => event.docs.map((e) => ViewModel.fromJson(e.data())).toList(),
        ));
  }).toList();

}

Upvotes: 0

Views: 501

Answers (1)

Juancki
Juancki

Reputation: 1872

you can create Streams in multiple ways,

Using yield Basic example:

Stream<List<int>> countStream(int to) async* {
  for (int i = 1; i <= to; i++) {
    List<int> list = List<int>();
    yield list;
  }
}

Now with firebase firestore and transforming the stream:

Stream<List<ViewModel>> matches(uid) async* {
   Stream<List<String>> docIds = FirebaseFirestore.instance
      .collection('matches')
      .where('match', arrayContains: uid)
      .snapshots()
      .map((event) => event.docs.map((e) => e.id).toList());
   await for (List<ViewModel> chunk in source) {
       // Do other operations if you need
       yield chunk
   }
}

Upvotes: 2

Related Questions