Dart Flutter Firebase FirebaseException ([cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.)

Issue

I have the following code in my main.dart file:

  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );

  FirebaseFirestore firestore = FirebaseFirestore.instance;
  await firestore.collection("oneSignal").add(
    {
      "id": "testing"
    }
  );

When I run the codes, I get an error like this:

enter image description here

What is the problem? How can I solve it?


enter image description here


Apart from the main.dart file, I also have a file related to Firebase:

For Firebase, apart from the main.dart file, I also have a file called firebase_options.dart. It contains many IDs.

Solution

In firebase security is handled by security rules, it’s a tab in your firestore firebase console. Check there if your caller (authenticated or not authenicated) is able to do write operation into oneSignal collection

For authenticated

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

For unauthenticated

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

Answered By – Dominik Šimoník

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *