FIREBASE FATAL ERROR: Database initialized multiple times

  1. I am getting Error message when I try to run a flutter wed on chrome. I an new to flutter, I am working on an existing project I transfer from another computer in my work place. the project was working fine on the previous machine. I use vs code.
  • Details about my goal: There is a file called firebaseCustom that contain custom code consuming firebase database which is called from multiple pages, when I tried to run it it shows error message as below. How do I initialize so that the error will be resolve?

  • Expected Results: To get the project compiled and run.

  • Actual Results: I am getting error message.

  • Error Messages:

Blockquote Error: FIREBASE FATAL ERROR: Database initialized multiple times. Please make sure the format of the database URL matches with each database() call.

  1. What have I tried so far - I have tried to to put the setting of the debug device “chrome” in order.

  2. Here’s the minimum code you would need to reproduce the problem -
    This is the "firebaseCustom.dart"
    ===========================================
    import ‘package:firebase/firebase.dart’ as fb;

import ‘package:firebase/firebase.dart’;

//Replace These Values With Yours

class FirebaseHelper {

static fb.Database initDatabase() {

try {

  if (fb.apps.isEmpty) {

    fb.initializeApp(

        apiKey: "AIxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxpaS8",

        authDomain: "my-name.firebaseapp.com",

        databaseURL: "https://my-name-default-rtdb.firebaseio.com",

        projectId: "my_name",

        storageBucket: "my_name.appspot.com",

        messagingSenderId: "63xxxxxxxxxx27",

        appId: "1:634xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx6");

  }

} on fb.FirebaseJsNotLoadedException catch (e) {

  print(e);

}

return fb.database();

}

}

// ignore: camel_case_types

class fire {

static fb.Database database = FirebaseHelper.initDatabase();

}

Future getOnce(fb.DatabaseReference AdsRef) async {

String a;

await AdsRef.once(‘value’).then((value) => {a = value.snapshot.val()});

return a;

}

Future getList(fb.DatabaseReference AdsRef) async {

List list = [""];

await AdsRef.once(‘value’)

  .then((value) => {list = result(value.snapshot, list)});

return list;

}

List result(DataSnapshot dp, List list) {

list.clear();
dp.forEach((v) {
list.add(v);
});
return list;
}

==============
This is some of the areas at which I call the firebase database from.

void getSnapshotList() async {

fb.Database db = fb.database();

DatabaseReference dataRef = db.ref('handyman');

dataRef.onValue.listen((event) {

  DataSnapshot snapshot = event.snapshot;

  if (snapshot == null) {

    print("Result is Empty");

    return;

  }

  //String totalEarnings = snapshot.val().toString();

  Map<dynamic, dynamic> earningsInfo = snapshot.val();

  print("Earnings");

  //print(totalEarnings);

========
Another Page

void registerNewUser(BuildContext context) {

fb.Database db = fb.database();

DatabaseReference dataRef = db.ref('handyman');

fb.UserCredential result;

fb.User user = result.user;

Map userDataMap = {

  "name": nameTextEditingController.text.trim(),

  "phone": phoneTextEditingController.text.trim(),

  "maritalStatus": statusTextEditingController.text.trim(),

  "age": ageTextEditingController.text.trim(),

  "stateOfOrigin": originTextEditingController.text.trim(),

  "lga": localTextEditingController.text.trim(),

  "residential": residentialTextEditingController.text.trim(),

  "occupation": selectedOccupationType,

  "office": officeTextEditingController.text.trim(),

  "meansOfID": idMeansTextEditingController.text.trim(),

  "cardNo": cardTextEditingController.text.trim(),

  "type": selectedOccupationType

};

dataRef.child(user.uid).set(userDataMap);

updateBankDetails();

}

Yes!
After more searching, I got the solution to my issue, I had to downgrade the fairebase_database pub, and the error cleared.