【问题标题】:Flutter/Dart: Awaiting a Stream Within a StreamFlutter/Dart:在流中等待流
【发布时间】:2020-09-12 19:26:47
【问题描述】:

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

问题是函数在流返回必要的值之前终止,并将它们添加到要合并和返回的流列表中,从而导致返回空值。我无法在流函数中放置等待语句,因此我不确定如何在最终返回之前等待内部流完成。感谢您提供解决此问题的任何和所有建议。

【问题讨论】:

标签: firebase flutter dart google-cloud-firestore stream


【解决方案1】:

流是异步数据事件。结果,第二个打印语句被执行,并且在添加流之后,执行内部打印。 Official docs might help you here

【讨论】:

  • await 语句不能放在流函数中,所以有没有办法在它完成后返回?将 return 移到里面是行不通的。
猜你喜欢
  • 2020-09-29
  • 2019-08-27
  • 2019-05-22
  • 2019-06-28
  • 2020-02-22
  • 1970-01-01
  • 2020-06-24
  • 2021-03-29
  • 2021-09-03
相关资源
最近更新 更多