Julien
Julien

Reputation: 4163

Flutter - how to write data in Firebase (realtime ddb) : permission denied

I would like to write data in Firebase and I get a permission error, here is what I tried :

void initState() {
    super.initState();
    testFirebase();
 }

 Future testFirebase() async {

    initUser();

    //Initialize Firebase
    final FirebaseApp firebaseApp = await FirebaseApp.configure( ... );

    final FirebaseDatabase database = new FirebaseDatabase(app: firebaseApp);

    database.reference().child('counter').push().set(<String, String>{
      'var': 'test'
    });
  }

  Future initUser() async {
    googleUser = await _ensureLoggedInOnStartUp();
    if (googleUser == null) {
       setState(() {
         state.isLoading = false;
       });
    } else {
       var firebaseUser = await logIntoFirebase();
    }
  }

Here is my Firebase rules :

enter image description here

The google-services.json file is added to the app root directory :

enter image description here

Result :

I get the following error message :

enter image description here

I tried also with :

push().setValue('2')

but it doesn't work and that makes me crazy, I don't understand...

Any idea?

Upvotes: 0

Views: 3808

Answers (3)

Teddy
Teddy

Reputation: 1

I was able to fix this problem by not accessing the database reference using the method listed in the plugin documentation:

final FirebaseDatabase database = FirebaseDatabase(app: app);
this.databaseRef = database.reference();

But instead making use of this approach:

this.databaseRef = FirebaseDatabase.instance.reference();

When you work with multiple pieces of Flutter documentation, you will see that many approaches work for reading public information from the Real Time Database, but only one method worked for reading information that depends on the auth variable. This was my experience.

Working method: https://marcinszalek.pl/flutter/firebase-database-flutter-weighttracker/

Non-working method was associated with the plugin documentation: https://pub.dartlang.org/packages/firebase_database#-example-tab-

Also, my project was configured using the main Firebase Flutter documentation for linking my app to Firebase: https://firebase.google.com/docs/flutter/setup

Upvotes: 0

Julien
Julien

Reputation: 4163

Just adding await to initUser() didn't work, the database was also not correctly adressed.

Solution :

FirebaseDatabase.instance.reference().child ...

instead of :

final FirebaseApp firebaseApp = await FirebaseApp.configure( ... );

final FirebaseDatabase database = new FirebaseDatabase(app: firebaseApp);

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598623

Quick initial check is that you need to await initUser(). So:

Future testFirebase() async {

    await initUser();

    //Initialize Firebase
    final FirebaseApp firebaseApp = await FirebaseApp.configure( ... );

Without that I'd expect the calls to the database to start before auth has finished.


Update: I just verified that is indeed the case with this simple code:

void _signin() async {
  print("Calling __actuallySignin");
  __actuallySignin();
  print("called __actuallySignin and waited");
}
void __actuallySignin() async {
  print("Calling signIn...");
  await FirebaseAuth.instance.signInAnonymously();
  print("called signIn... and waited");
}

This prints:

flutter: Calling __actuallySignin
flutter: called __actuallySignin and waited
flutter: Calling signIn...
...
flutter: called signIn... and waited

So the __actuallySignin method is done before the sign in is done. To make the calling code wait for the result you add await:

await __actuallySignin();

Which outputs:

flutter: Calling __actuallySignin
flutter: Calling signIn...
flutter: called signIn... and waited
flutter: called __actuallySignin and waited

Upvotes: 2

Related Questions