【问题标题】:Firebase DocumentSnapshot is always nullFirebase DocumentSnapshot 始终为空
【发布时间】:2019-04-12 11:53:44
【问题描述】:

我正在尝试在 Firebase 中获取文档的数据。我正在使用这个功能:

DocumentSnapshot docRef = 
await Firestore.instance.collection("products").document("SF").get();

print(docRef.exists);

即使文档确实存在,docRef.exists 也会返回“false”。

我认为这与身份验证流程有关,系统无法识别登录用户。

print(FirebaseAuth.instance.currentUser());

导致 'Future ' 的实例。

知道如何解决这个问题吗?

最好的问候

编辑:

这是我在 firebase 中的规则:

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

【问题讨论】:

  • 如果你把安全规则中的read操作设为public,那它还能用吗?
  • @CopsOnRoad 我编辑了我的问题。规则是公开的。
  • 你能像这样使用等待,没有异步吗?
  • await 在异步函数中:data.documents.forEach((DocumentSnapshot doc) async { .... })
  • 我找到了问题的解决方案,但现在有另一个问题...stackoverflow.com/questions/55652643/… 我以为它与 document("SF") 部分有关,但事实并非如此。该线程可以关闭。

标签: firebase dart flutter google-cloud-firestore


【解决方案1】:

您定义安全规则的方式不正确。更改您的安全规则。

match /products/{document=**}  { 
     allow read, write: if true;
 } 

【讨论】:

  • 我更改了安全规则,但现在应用程序抛出“PlatformException(执行getDocuments时出错,PERMISSION_DENIED:缺少或不足的权限。,null)”
  • 我更新了我的答案。检查并更改您的安全规则。现在它可以工作了
  • 还是同样的错误...我再等几分钟再试一次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 2020-02-18
  • 2017-07-31
相关资源
最近更新 更多