Flutter流每个应用程序启动仅工作一次

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

当我第一次在Android模拟器中运行该应用程序,然后将其替换为空之前,在无法返回任何内容直到模拟器停止并重新启动之前,我有一个工作流。让我再次输出任何内容的唯一方法是关闭模拟器并重新启动它。如果异步函数代替流而不是简单地运行一次并在重新构建它所属的小部件时返回​​正确的内容,我将感到非常高兴。感谢您为解决此问题提供的任何帮助。

  Stream<List<Memo>> getFeed() async* {

    List<Stream<List<Memo>>> streams = [];

    List<String> friends = await Firestore.instance.collection("users")
    .document(userid)
    .collection("friends")
    .snapshots().map(_snapshotToStringList).first;

      for (var i = 0; i < friends.length; i++) {
        streams.add(Firestore.instance.collection("memos")
        .where("owner", isEqualTo: friends[i])
        .snapshots()
        .map(_snapshotToMemoList));
      }

    yield* StreamGroup.merge(streams);
  }

接收到流

    return StreamProvider<List<Memo>>.value(
      value: dbService( user: widget.user ).getFeed(),
      child: SafeArea(
        child: TestList(),
      )
    );

然后在TestList中是

@override
  Widget build(BuildContext context) {
    final memos = Provider.of<List<Memo>>(context);
    print(memos);
    return (memos == null || memos.length == 0) ? Text('no content') :
        ListView.builder(
          itemCount: memos.length,
          itemBuilder: (BuildContext context, int index) {
          return Text(memos[index].body);
          }
        );
  }

我是扑扑/飞镖的新手,所以在您的建议/解释中,请先假设一点先验知识,或者理想情况下,请更正我上面的代码

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

请尝试替换此

 for (var i = 0; i < friends.length; i++) {
        streams.add(Firestore.instance.collection("memos")
        .where("owner", isEqualTo: friends[i])
        .snapshots()
        .map(_snapshotToMemoList);
      }

带有以下内容

yield Firestore.instance
        .collection('memos')
        .where('owner', whereIn: friends)
        .snapshots()
        .map(_snapshotToMemoList));

并且您对final memos = Provider.of<List<Memo>>(context);行有问题,因为当未获取响应且请求处于待处理状态时,它将把数据作为null。尝试使用StreamBuilder处理此问题。检查this

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