扑扑/飞镖列表撤消追加

问题描述 投票: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);
}

问题是,.listen函数中的print语句显示'streams'包含2个项目,但是它外面的print语句(返回之前的行)说streams是空的,意味着一个空列表回。有趣的是,即使稍后在函数中出现,也会先输出后面的print语句。感谢您提供解决此问题的所有建议。

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

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

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