【问题标题】:Not receiving data from Cloud Firestore but there is the data未从 Cloud Firestore 接收数据,但有数据
【发布时间】:2021-04-29 17:54:26
【问题描述】:

我正在尝试从 Cloud Firestore 制作一个简单的流。我的 Firestore 数据中有内容,但 Stream 似乎不起作用。
这就是我构建 Stream 的方式:

body: StreamBuilder(
        stream: Firestore.instance.collection('user_data').document('rCqMQ3oLjBLk6yg6P1oT').collection('Buttons').snapshots(),
        builder: (context, snapshot) {
          if (snapshot.hasError){
            return Container(color: Colors.red);
          }
          if (!snapshot.hasData){
            return Center(child: CircularProgressIndicator());
          }
          if (snapshot.hasData){
            return GridView.count(
              padding: EdgeInsets.all(15),
              crossAxisSpacing: 20.0,
              mainAxisSpacing: 20.0,
              crossAxisCount: 3,
              children: [
                CreateCard("ola"),
                GestureDetector(
                  onTap: () async{
                    print(snapshot.data);
                  },
                  child: Container(
                  color: Colors.black,
                  width: 150,
                  height: 150,
                  child: Icon(Icons.add, color: Colors.white,),
                  ),
                ),
              ],
            );
          }
        }

但它总是进入 ``ìf(!snapshot.hasData)```
数据库图片:

【问题讨论】:

  • 你的代码看起来不错;在流进入active 状态后,snapshot.data 应该有DocumentSnapshot 的列表。您是否尝试过检查 connectionstate 而不是 hasData 布尔值,例如 - api.flutter.dev/flutter/widgets/StreamBuilder-class.html 中给出的示例代码
  • 我已经创建了连接状态,但是它进入了等待状态。我打印了 snapshot.data 并收到了 null
  • 如果卡在等待状态,可能是网络/连接问题..您只能在active or done 状态下获得数据snapshot.data..
  • 但是我怎么会遇到连接问题?
  • 可能是个例.. 设备网络稳定吗?

标签: flutter dart google-cloud-firestore


【解决方案1】:

您是否设置了安全规则?有时我会遇到这个问题而没有从 firebase 获得异常

现在尝试将此添加到安全规则中。

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

注意:稍后设置一些规则,这会使您的数据库广泛开放。 https://medium.com/@khreniak/cloud-firestore-security-rules-basics-fac6b6bea18e

【讨论】:

  • 安全规则尚未设置。
  • 这可能是它没有加载的原因。您可以发布它们或尝试我在更新帖子中使用的那些吗?
  • 我会按照@MuthuTavamami 的建议去做,如果不行我会提供反馈
  • 我仍然有错误,我会尝试你的修复
猜你喜欢
  • 1970-01-01
  • 2021-05-01
  • 1970-01-01
  • 2018-10-26
  • 2021-09-03
  • 2013-05-11
  • 2022-01-11
  • 2017-05-20
相关资源
最近更新 更多