翩翩镖局。等待流中流

问题描述 投票:0回答:1

我在flutter里有一个流,它从firestore数据库中批量请求项目,合并它们并返回合并结果。代码如下。

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);
}

问题是该函数在流返回必要的值之前就终止了,并将它们添加到要合并和返回的流列表中,导致返回为空。我不能在流函数中放入 await 语句,所以我不确定如何在最终返回之前等待内部流的完成。任何和所有的建议在解决这个问题是感激。

firebase flutter dart google-cloud-firestore stream
1个回答
0
投票

流是异步数据事件。因此,第二条打印语句被执行,在流被添加后,内部打印被执行。官方文档可能会在这里帮助你

© www.soinside.com 2019 - 2024. All rights reserved.