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

问题描述

我的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; }}}

解决方法

已编辑!!!

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;
    }
  }
}

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

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...