firebase - 为什么我的快照显示它包含数据?

标签 firebase dart flutter google-cloud-firestore

我有一个流生成器:

 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/

相关文章:

flutter - 使用 flutter 动画将图标旋转 180 度

flutter - 在隔离中使用类的特定实例

Flutter 和 HTML 字符代码

dart - 如何在 Dart 中调用与变量同名的顶级函数?

github - 如何直接从 git master 分支部署 Web 应用程序

flutter - Flutter 中的 Android LiveData 相当于什么?

firebase - 如何最大限度地减少 Firebase 函数延迟

c# - Firebase Unity 密码身份验证失败

javascript - 我如何获取集合中文档中的所有子集合

swift - 从 firebase swift 获取项目列表