【问题标题】:PlatformException(Error performing setData, PERMISSION_DENIED: Missing or insufficient permissions., null)PlatformException(执行 setData 时出错,PERMISSION_DENIED:权限缺失或不足。,null)
【发布时间】:2020-09-10 09:13:00
【问题描述】:

我的 Dart 代码没有错误,但我无法将数据写入 Firestore, 我无法将数据写入云 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; }}}

【问题讨论】:

    标签: firebase flutter dart google-cloud-firestore


    【解决方案1】:

    已编辑!!!:

    旧:

    尝试将您的 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;
        }
      }
    }
    

    抱歉编辑晚了,祝大家编码愉快!

    【讨论】:

    • 将其标记为其他人可以查看的答案 :)
    猜你喜欢
    • 2021-01-10
    • 2020-09-25
    • 2019-01-06
    • 2019-05-05
    • 2020-08-14
    • 2020-09-06
    • 2018-08-24
    • 2020-02-11
    • 2019-10-24
    相关资源
    最近更新 更多