flutter - 用户第一次登录flutter firestore时如何处理空值
问题描述
您好我正在构建一个应用程序,用户将在其中添加某些项目,这些项目将显示在网格视图中。但是当他们注册时,在那个阶段他们还没有在数据库中添加任何东西,所以 firestore 返回空值。
这是一个新用户,因此 firestore 中没有当前用户的数据
我正在使用以下方法检查空值:
builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData || !snapshot.data!.exists) {}
但是我仍然收到以下错误,因为它没有处理这种情况:
The following NoSuchMethodError was thrown building
StreamBuilder<DocumentSnapshot<Object?>>(dirty,
state: _StreamBuilderBaseState<DocumentSnapshot<Object?>,
AsyncSnapshot<DocumentSnapshot<Object?>>>#3898f):
The method '/' was called on null.
Receiver: null
Tried calling: /(60)
The relevant error-causing widget was:
StreamBuilder<DocumentSnapshot<Object?>>
解决方案
将此作为社区 Wiki 发布,因为它基于 @esentis 评论。
您必须使用以下代码检查快照中的数据是否为空:
if (!snapshot.hasData){
return Center(child: CircularProgressIndicator());
}
推荐阅读
- typescript - 为什么这个 for 循环不在打字稿函数中执行?
- javascript - 从 ReactJS 中的 AWS S3 存储读取 .txt 文件
- python - 如何从 txt 文件中快速加载面部特征(作为 np.ndarray)
- haxe - 在 HaxeFlixel 中完成一个动作之前如何等待一秒钟?
- python - 在同一个 Python 脚本中导入两个版本的库?
- ios - UITabBarAppearance 在 iOS15 iPad 上不起作用(标题颜色)
- python - 使用行的自定义字段向 Pymongodb 插入记录时避免重复
- html - CSS子元素与后代选择器实现问题
- python - 无法 conda 安装 chromedriver
- elasticsearch - 通过 Nest 客户端运行查询与查询搜索查询?