简体   繁体   English

Flutter-如何在Firebase(实时DDB)中写入数据:权限被拒绝

[英]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 : 我想在Firebase中写入数据,但出现权限错误,这是我尝试的方法:

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 : 这是我的Firebase规则:

在此处输入图片说明

The google-services.json file is added to the app root directory : google-services.json文件已添加到应用的根目录:

在此处输入图片说明

Result : 结果:

I get the following error message : 我收到以下错误消息:

在此处输入图片说明

I tried also with : 我也尝试过:

push().setValue('2') 推()。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() . 快速的初步检查是您需要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. 因此, __actuallySignin方法在登录之前完成。 To make the calling code wait for the result you add await : 要使调用代码等待结果,请添加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. 只是将await添加到initUser()无效,数据库也不正确。

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. 当您使用多篇Flutter文档时,您会发现有很多方法可用于从实时数据库中读取公共信息,但是只有一种方法可用于读取依赖于auth变量的信息。 This was my experience. 这是我的经验。

Working method: https://marcinszalek.pl/flutter/firebase-database-flutter-weighttracker/ 工作方法: 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- 无效的方法与插件文档相关联: 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 另外,我的项目是使用Firebase Flutter主要文档配置的,用于将我的应用程序链接到Firebase: https ://firebase.google.com/docs/flutter/setup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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