无法从Firebase检索数据库实例

时间:2020-08-11 07:42:14

标签: database firebase flutter dart collections

由于某些原因,我无法从Firebase数据库检索数据。请看看我的代码和规则。谢谢

我的代码

List<EvaluatingImage> imageInfo = [];

  Future<Query> queryUsers() async {
    // DatabaseReference referenceData =
    //     FirebaseDatabase.instance.reference().child('pictures');
    DatabaseReference referenceData =
        FirebaseDatabase.instance.reference().child('users');
    referenceData.once().then((DataSnapshot dataSnapShot) {
      Map<dynamic, dynamic> values = dataSnapShot.value;
      values.forEach(
        (key, value) {
          print(value['email']);
        },
      );
    });
  }

规则

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    // This rule allows anyone with your database reference to view, edit,
    // and delete all data in your Firestore database. It is useful for getting
    // started, but it is configured to expire after 30 days because it
    // leaves your app open to attackers. At that time, all client
    // requests to your Firestore database will be denied.
    //
    // Make sure to write security rules for your app before that time, or else
    // all client requests to your Firestore database will be denied until you Update
    // your rules
    match /{document=**} {
      //allow read, write: if request.time < timestamp.date(2020, 9, 28);
      allow read, write : if true;
    }
  }
}

相反,我收到此错误

W/SyncTree(29984): Listen at /users failed: DatabaseError: Permission denied
E/flutter (29984): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: DatabaseError(-3, Permission denied, )
E/flutter (29984):

请帮助我!非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

以上规则适用于使用Firebase实时数据库的firestore,您需要将实时数据库的规则更改为以下内容:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

注意:以上规则将使所有用户都可以从数据库读取和写入数据,仅使用上述规则进行开发

https://firebase.google.com/docs/database/security