在Flutter上使用Firebase数据库拒绝权限

Dan*_*eel 1 dart firebase firebase-realtime-database flutter

我的团队正在尝试在Flutter应用中使用Firebase实时数据库。他们更改了pubspec.yaml和两个build.gradle文件,以及链接的the google-services.json文件,如在线教程中所示。但是,这些教程似乎相互矛盾(在它们之间,甚至与文档之间)。

这是他们代码的相关部分:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_core/firebase_core.dart';

class ProgramPage extends StatefulWidget {
  @override
  _ProgramPageState createState() => new _ProgramPageState();
}

class _ProgramPageState extends State<ProgramPage> {
  List<Conference> _conferences = List();
  DatabaseReference itemRef;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  static Future<FirebaseApp> _getApp() async {
    const FirebaseOptions myOptions = const FirebaseOptions(
      googleAppID: '<id>',
      apiKey: '<key>',
      databaseURL: '<url>',
    );
    FirebaseApp app;
    app = await FirebaseApp.configure(name: "conferences", options: myOptions);
    return app;
  }

  @override
  void initState() {
    super.initState();
    _getApp().then((app) {
      final FirebaseDatabase database = new FirebaseDatabase(app: app);
      itemRef = database.reference().child('conferences');
      itemRef.onChildAdded.listen(_onEntryAdded);
      itemRef.onChildChanged.listen(_onEntryChanged);
    });
  }

  _onEntryAdded(Event event) {
    setState(() {
      _conferences.add(Conference.fromSnapshot(event.snapshot));
    });
  }

  _onEntryChanged(Event event) {
    var old = _conferences.singleWhere((entry) {
      return entry.id == event.snapshot.key;
    });
    setState(() {
      _conferences[_conferences.indexOf(old)] =
          Conference.fromSnapshot(event.snapshot);
    });
  }

  // the build method basically returns a ListView of Conference objects
}
Run Code Online (Sandbox Code Playgroud)

但是,当他们在Android模拟器上运行它时,会出现以下错误:

W/SyncTree( 7764): Listen at /conferences failed: DatabaseError: Permission denied
E/flutter ( 7764): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 7764): Instance of 'DatabaseError'
E/flutter ( 7764): #0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:1114:29)
E/flutter ( 7764): #1      _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 7764): #2      _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
E/flutter ( 7764): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 7764): Instance of 'DatabaseError'
E/flutter ( 7764): #0      _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:1114:29)
E/flutter ( 7764): #1      _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 7764): #2      _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
Run Code Online (Sandbox Code Playgroud)

您能向我们解释什么地方错了吗?

编辑:安全规则:

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑2:我们正在使用Santosh Anand的解决方案来简化规则(在此项目中安全性不是大问题)。现在我们收到以下消息:

W/zygote  (14174): Unsupported class loader
W/zygote  (14174): Skipping duplicate class check due to unsupported classloader
I/DynamiteModule(14174): Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:6
I/DynamiteModule(14174): Selected remote version of com.google.android.gms.firebase_database, version >= 6
W/zygote  (14174): Unsupported class loader
W/zygote  (14174): Skipping duplicate class check due to unsupported classloader
D/NetworkSecurityConfig(14174): No Network Security Config specified, using platform default
I/zygote  (14174): Do partial code cache collection, code=29KB, data=26KB
I/zygote  (14174): After code cache collection, code=29KB, data=26KB
I/zygote  (14174): Increasing code cache capacity to 128KB
Run Code Online (Sandbox Code Playgroud)

San*_*and 5

您必须使用Firebase验证您的应用程序。

要么

您可以更改安全规则,例如

{
  "rules": {
    ".read": true,
    ".write": true
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 您应该知道,通过这样的配置,每个人都可以写入、删除或更改 Firebase 数据库中的数据,您必须为此支付 Firebase 配额。 (6认同)

Min*_*rid 5

以下步骤为我解决了问题:

1-在创建实时数据库时启用测试模式。

2-使默认数据库是实时数据库。

3- 在数据库选项卡中更改规则如下

{
  "rules": {
    ".read": true,
    ".write": true
  }
}
Run Code Online (Sandbox Code Playgroud)

4- 从身份验证选项卡启用匿名登录。

在那之后,它不会修复,直到我使用“flutter clean”选项

-> 在 android studio 中:转到工具-> Flutter->flutter clean