【问题标题】:Exception has occurred. FirebaseException ([cloud_firestore/permission-denied]发生异常。 FirebaseException([cloud_firestore/permission-denied]
【发布时间】:2021-09-17 12:35:06
【问题描述】:

发生了异常。 FirebaseException([cloud_firestore/permission-denied]调用者没有执行指定操作的权限。)

 ~ master ● flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, 2.3.0-24.1.pre, on macOS 12.0 21A5268h darwin-x64, locale en-RU)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2020.3.3)
[✓] VS Code (version 1.57.1)
[✓] Connected device (3 available)

• No issues found!

部分代码

...
    DocumentSnapshot snapshot = await categories.doc('categories').get();
...

Cloud Firestore 规则

rules_version = '2';
// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

存储规则

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

【问题讨论】:

    标签: firebase flutter google-cloud-firestore firebase-security


    【解决方案1】:

    您的安全规则要求用户必须先登录 Firebase 身份验证,然后才能读取和写入数据。错误消息表明不满足此条件。

    您共享的代码无法表明用户确实已登录,因此我建议您先添加一些额外代码来帮助解决此问题。例如:

    assert(FirebaseAuth.instance.currentUser != null, "User must be signed in before they can read from Firestore");
    DocumentSnapshot snapshot = await categories.doc('categories').get();
    

    【讨论】:

    • 谢谢你的回答,伙计。是的,用户没有登录。顺便说一句,我能够在它发生之前登录。 pastebin.com/nZ6JDaqs。 _AssertionError('package:foodshare/services/categoryservice.dart':断言失败:第 21 行 pos 12:'FirebaseAuth.instance.currentUser != null':用户必须先登录才能从 Firestore 读取数据)
    • 整个代码库:github.com/organicnz/foodshare
    • 你的仓库是私有的
    • 是的,我很快就意识到了这一点并且已经改变了:)
    猜你喜欢
    • 2021-07-25
    • 1970-01-01
    • 2022-08-10
    • 2022-11-14
    • 2020-11-29
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 2022-08-06
    相关资源
    最近更新 更多