Reputation: 13
No error in my Dart Code, but I cannot write data to firestore, i cannot write data to cloud firestore, no error in my code: but error message comes again and again Error is as Below::
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)
Code Snippet:
```
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:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{uid}/Reports/{document=**} {
allow read, write; }}}
Upvotes: 1
Views: 455
Reputation: 1349
EDITED!!!:
OLD:
Try changing your Firestore rule to:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{uid}/Reports/{document=**} {
allow read, write: if true;
}
}
}
The only thing that was really changed here is this line:
allow read, write: if true;
If that does not work, try:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
NEW:
My first answer was actually insecure, because using the
if true
allowed anyone with access to your firebase project to have admin rights. Rather use this instead, which only works if the request from the app is authenticated:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}
Sorry for the late edit, have a great time coding everyone!
Upvotes: 1