代码之家  ›  专栏  ›  技术社区  ›  Julien

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

  •  0
  • Julien  · 技术社区  · 6 年前

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

    enter image description here

    enter image description here

    结果:

    我收到以下错误消息:

    enter image description here

    我还尝试了:

    你知道吗?

    3 回复  |  直到 6 年前
        1
  •  2
  •   puf - Frank van Puffelen    6 年前

    快速初步检查是你需要 await initUser() . 所以:

    Future testFirebase() async {
    
        await initUser();
    
        //Initialize Firebase
        final FirebaseApp firebaseApp = await FirebaseApp.configure( ... );
    

    否则,我希望对数据库的调用在auth完成之前启动。


    :我刚刚验证了以下简单代码的情况:

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

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

    所以 __actuallySignin 方法在登录完成之前完成。使调用代码等待您添加的结果 await

    await __actuallySignin();
    

    输出:

    flutter: Calling __actuallySignin
    flutter: Calling signIn...
    flutter: called signIn... and waited
    flutter: called __actuallySignin and waited
    
        2
  •  1
  •   Julien    6 年前

    解决方案:

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

    而不是:

    final FirebaseApp firebaseApp = await FirebaseApp.configure( ... );
    
    final FirebaseDatabase database = new FirebaseDatabase(app: firebaseApp);
    
        3
  •  0
  •   Teddy    5 年前

    我可以通过不使用插件文档中列出的方法访问数据库引用来解决此问题:

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

    而是利用这种方法:

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

    工作方法: https://marcinszalek.pl/flutter/firebase-database-flutter-weighttracker/

    非工作方法与插件文档相关联: https://pub.dartlang.org/packages/firebase_database#-example-tab-

    此外,我的项目是使用主Firebase颤振文档配置的,用于将我的应用程序链接到Firebase: https://firebase.google.com/docs/flutter/setup