我有一个流生成器:
StreamBuilder(
stream: Firestore.instance
.collection('stripe_customers')
.document(userId)
.collection('sources')
.document('source')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return new Text("hello");
}else {
var userDocument = snapshot.data;
return new Text(userDocument["card"]['exp_year'].toString());
}
},
),
当 collection('sources')
不存在时,我希望显示 hello
而不是
{
var userDocument = snapshot.data;
return new Text(userDocument["card"]['exp_year'].toString());
}
代码被执行。 firestore 上不存在该集合...所以我想知道为什么会这样?
最佳答案
您可能得到一个空列表 []
但它不是 null
因此 hasData
设置为 true。
你应该检查结果的长度:
if (!snapshot.hasData || snapshot.data.length == 0) {
// Nothing found here...
}
关于firebase - 为什么我的快照显示它包含数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56237070/