当我第一次在我的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);
}
);
}我对飞镖并不熟悉,所以在你的建议/解释中,请假定你之前所掌握的知识很少,或者理想的情况下,请更正我上面的代码。
发布于 2020-05-28 00:32:01
请试着更换这个
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来处理这个问题。检查这
https://stackoverflow.com/questions/62054853
复制相似问题