PlatformException(执行setData时出错,PERMISSION_DENIED:权限缺失或不足。,null)

时间:2020-09-10 09:13:00

标签: firebase flutter dart google-cloud-firestore

我的Dart代码中没有错误,但是我无法将数据写入Firestore, 我无法将数据写入Cloud Firestore, 我的代码中没有错误:但是错误消息反复出现 错误如下:

  W/Firestore( 9874): (21.3.0) [Firestore]: Write failed at user/6nattT06v0dfeeb6Rm9ablZuijU2/reports/reports_1: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}
E/flutter ( 9874): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(Error performing setData, PERMISSION_DENIED: Missing or insufficient permissions., null)

代码段:

```

abstract class Database {
    Future createReport(Map<String, dynamic> reportData);
    }
    
    class FirestoreDatabase implements Database{
    FirestoreDatabase({@required this.uid}) : assert (uid!=null);
    final String uid;
    
    //TODO: to create a report hard coded:
    Future createReport(Map<String, dynamic> reportData) async {
    final path = 'user/$uid/reports/reports_1';
    final documentReference = Firestore.instance.document(path);
    await documentReference.setData(reportData);
    }
    }
    void _createReport(BuildContext context) { final database = Provider.of<Database>(context); database.createReport({ 'crime Type' : 'Blogging', 'Description' : 'description needs to be provided', }); }```
    
    ```class Report{
    Report({@required this.crimeType, @required this.description});
    final String crimeType;
    final String description;
    //TODO: to map data to Firestore
    Map<String, dynamic> toMap(){
    return{
    'CrimeType' : crimeType,
    'Description': description,
    };
    }
    }```

Firebase规则:

rules_version = '2'; 
service cloud.firestore {
 match /databases/{database}/documents {
 match /users/{uid}/Reports/{document=**} {
 allow read, write; }}}

1 个答案:

答案 0 :(得分:0)

已编辑!!!

OLD:

尝试将您的Firestore规则更改为:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{uid}/Reports/{document=**} {
      allow read, write: if true;
    }
  }
}

唯一真正更改的地方是此行:

allow read, write: if true;

如果这不起作用,请尝试:

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

新:

我的第一个答案实际上是不安全的,因为使用

if true

允许有权访问您的firebase项目的任何人拥有管理员权限。而是使用它,它仅在应用程序的请求经过身份验证时才有效:

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

对不起,您编辑晚了,祝大家编码愉快!