flutter - StreamBuilder 快照是否为空
问题描述
StreamBuilder(
stream: firestore
.collection('Product')
.doc(order.productId)
.collection('AbleToReview')
.doc(auth.currentUser.uid)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: CircularProgressIndicator(),
);
}
if (snapshot.data==null){
return Text('no data');
} else {
return Text('have data');
}
}
)
有时firestore.collection('Product').doc(order.productId).collection('AbleToReview').doc(auth.currentUser.uid).snapshots()查询中不存在数据。由于取决于数据是否存在,我尝试重新使用不同的小部件。但snapshot.data==null
或!snapshot.hasData
不是我想要的正确方式。
解决方案
您可以使用来检查查询中的实际数据是否为空snapshot.data.documents.isEmpty()
。
您可以将代码更新为以下代码:
if (snapshot.data==null || snapshot.data.data().isEmpty()){
return Text('no data');
} else {
return Text('have data');
}
推荐阅读
- json - 尝试在命令行中创建 AWS 执行角色时出现 JSON 错误
- c# - 在每条记录的 json 中添加值
- android - 如何以管理员身份在 Android 手机上删除应用程序
- scala - 在两个 mixin 的交集处覆盖 trait 函数
- javascript - 将数组合并到一个字符串,但在一个整数值处停止
- oracle - 如何在 Oracle 中为物化视图设置 ALL_ROWS?
- java - 从 Python 反序列化 Java 中的对象
- python - discord.py 如何为单独的服务器获取单独的前缀,其中 te 前缀存储在 sqlite 数据库中
- linux - 如何让 i2c 触摸设备在 linux 上工作?
- asp.net-core - 使用相同应用程序池的 IIS Web 应用程序是否在内存中共享 DLL?