简体   繁体   中英

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 :

在此处输入图片说明

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

在此处输入图片说明

Result :

I get the following error message :

在此处输入图片说明

I tried also with :

push().setValue('2')

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

Any idea?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM