0
votes

I have a stream within flutter that batch requests items from a firestore database, merges them and returns the merge. The code is as follows.

Stream<List<Memo>> get memos {
  // list of streams to be merged
  List<Stream<List<Memo>>> streams = [];
  // get list of friend userids
  Firestore.instance.collection("users").document(userid).collection("friends").snapshots().map(_snapshotToStringList).listen((friends){ // add where confirmed == true to this
    // break list into chunks of ten
    for (var i = 0; i < friends.length; i++) {
      // query post list for each chunk of 10 and add to stream list
      streams.add(Firestore.instance.collection("memos").where("userid", isEqualTo: friends.sublist(i, i + 1)).snapshots().map(_snapshotToMemoList));
    }
    print(streams);
    // merge steam list
  });
  print(streams);
  return StreamGroup.merge(streams);
}

The issue is that the function terminates before the stream returns the necessary values and adds them to the list of streams to be merged and returned resulting in a null return. I cannot put an await statement within a stream function so I am unsure on how to wait for the inner stream to complete before the final return. Any and all advice in fixing this issue is appreciated.

1
You should have a look at this question - Augustin R

1 Answers

0
votes

Streams are asynchronous data events. As a result, the second print statement gets executed and after the streams are added, the inner print is executed. Official docs might help you here