【发布时间】:2021-07-01 05:19:22
【问题描述】:
我查看了一些类似的问题,但没有找到我要查找的内容。
我有一个StreamBuilder,其快照没有数据,但如果我查看它正在侦听的集合,我可以看到有 2 个文档。
这是我第一次使用StreamBuilder,所以我可能遗漏了一些基本规则。
我说它是has no data,因为它总是显示"No chats here"。
这是代码(后面的流函数):
StreamBuilder<ChatCard>(
stream: widget.database.streamChats(widget.user),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Container(
child: Text(
"There's something here!",
style: TextStyle(
color: Colors.white,
),
),
);
} else {
return Container(
child: Text(
"No chats here",
style: TextStyle(
color: Colors.white,
),
),
);
}
},
),
这是流函数,有一些额外的检查,因为我试图找出什么不起作用,顺便说一句,try 块中似乎没有错误,也没有违反if 语句:
Stream<ChatCard> streamChats(MyUser sender) {
if (sender != null) {
if (sender.id != null) {
try {
FirebaseFirestore.instance
.collection("ids/tabs/${sender[0].toLowerCase()}/${sender.toLowerCase()}/chats")
.snapshots();
print(
"ids/tabs/${sender[0].toLowerCase()}/${sender.toLowerCase()}/chats'");
} on FirebaseException catch (e) {
print(e);
} catch (e) {
print(e);
}
} else
print("Sender ID null");
} else
print("Sender null");
}
如果您还可以向我提供一些关于StreamBuilder 和Stream 的信息,我会很乐意倾听,但显然这是一个加分项。
【问题讨论】:
-
如果在
if (snapshot.hasData) {之前调用print(snapshot);,你会看到什么? -
FirebaseFirestore.instance.collection("${sender.id}/chats'")假设这就是您将 collectionReference 构造为chats子集合的方式吗? -
@pskink 我看到了:颤振:AsyncSnapshot
(ConnectionState.none, null, null, null) -
ConnectionState.none没有别的?如果是这样,那么widget.database.streamChats(widget.user)不会发出任何数据 -
@pskink 不,只有我之前写的,没有和一些空字段
标签: firebase flutter google-cloud-firestore stream-builder