Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission denied using Firebase database with Flutter

My team is trying to use Firebase Realtime Database in a Flutter app. They changed the pubspec.yaml and both build.gradle files, and linked the google-services.json file as seen in online tutorials. However the tutorials seemed contradictory (between them and even with the documentation).

Here is the relevant part of their code :

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
}

But when they run it on an Android emulator, they get these errors :

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)

Could you explain us what is wrong ?

EDIT : Security rules :

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

EDIT 2 : We're using Santosh Anand's solution to simplify rules (security is not a big concern in this project). Now we get these messages :

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
like image 413
Daneel Avatar asked Jun 19 '18 09:06

Daneel


People also ask

Is Flutter good with Firebase?

One of the most reliable ways to combine the rapid development, user-friendly interface, and low maintenance costs of cross-platform solutions, is the usage of Flutter for the front-end and Firebase for the back-end.

How do I send data from Flutter to Firebase?

To use Flutter with Firebase, you will first need to set dependencies in the pubspec file. You will also have to import firestore , i.e., the database provided by Firebase for data handling. Now, import the Firebase dependencies into your Dart file. import 'package:cloud_firestore/cloud_firestore.

What is Firebase database in Flutter?

The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON and synchronized in realtime to every connected client. When you build cross-platform apps Flutter & Firebase, all of your clients can share one Realtime Database instance and automatically receive updates with the newest data.


2 Answers

You have to authenticate your application with firebase.

Or

you can make change into security rule like

{
  "rules": {
    ".read": true,
    ".write": true
  }
}
like image 85
Santosh Anand Avatar answered Oct 20 '22 00:10

Santosh Anand


The following steps solved the problem for me:

1-enable test mode when creating a real-time database.

2- make default database is the real-time database.

3- change the rule as following in database tab

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

4- enable anonymous login from the authentication tab.

after that, it does not fix until I used "flutter clean" option

-> in android studio: go to tools->Flutter->flutter clean

like image 25
Mina Farid Avatar answered Oct 20 '22 00:10

Mina Farid