1

I'm making an app that writes a waiting list in the firebase realtime databse, but when I try to run this simple bit of code, I get this error (Note- I'm running this on web):

class FirebaseMethods {
  FirebaseDatabase database = FirebaseDatabase.instance;

  void createPartyWaiting(String partyCode, String partyHostName) async {
    partyCode = partyCode.trim();
    DataSnapshot data = await database.ref("e").get();
    print(data.value);
  }
}

and i'm calling it as-

await FirebaseMethods().createPartyWaiting("PartyCode123", "Adam");

But when I call it, I get this error-

Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com
    at Re (https://www.gstatic.com/firebasejs/8.10.0/firebase-database.js:1:22344)
    at Ws (https://www.gstatic.com/firebasejs/8.10.0/firebase-database.js:1:148180)
    at au (https://www.gstatic.com/firebasejs/8.10.0/firebase-database.js:1:166314)
    at K.xu.INTERNAL.registerComponent.Y.setServiceProps.Reference [as instanceFactory] (https://www.gstatic.com/firebasejs/8.10.0/firebase-database.js:1:187315)

I'm initializing the web like this (I've censored the actual values)-

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (kIsWeb) {
    Firebase.initializeApp(
        options: const FirebaseOptions(
            apiKey: "apikey",
            authDomain: "authDomain",
            projectId: "projectId",
            storageBucket: "storageBucket",
            messagingSenderId: "messagingSenderId",
            appId: "appId"));
  }
  runApp(const MyApp());
}

I've also found out that there's this optional arguement to the initilaziation method-

enter image description here

But after adding my databaseUrl here, I got this error-

Error: [core/duplicate-app] A Firebase App named "[DEFAULT]" already exists
    at Object.throw_ [as throw] (http://localhost:53659/dart_sdk.js:5063:11)
at firebase_core_web.FirebaseCoreWeb.new.initializeApp (http://localhost:53659/packages/firebase_core_web/firebase_core_web.dart.lib.js:248:27)

What am I doing wrong, and how do I fix this? Thanks!

1
  • did you add JSON in your project ? Commented Feb 21, 2022 at 11:47

3 Answers 3

2

For anyone who has used $ flutterfire configure to configure firebase to your flutter project as shown in here before adding Realtime Database, you will have to run the command again to reconfigure your project or add "databaseURL" to the FirebaseOptions in your lib/firebase_options.dart.

Your lib/firebase_options.dart should look something like this:

// inside lib/firebase_options.dart

// ...

static const FirebaseOptions web = FirebaseOptions(
    apiKey: '...',
    appId: '...',
    messagingSenderId: '...',
    projectId: 'project-id',
    authDomain: 'project-id.firebaseapp.com',
    databaseURL: 'https://your-database-id.firebasedatabase.app', // IMPORTANT!
    storageBucket: 'project-id.appspot.com',
    measurementId: '...',
);

// ...
Sign up to request clarification or add additional context in comments.

Comments

0

Since you're configuring your Firebase project (for web) by passing the configuration data to Firebase.initializeApp, you should also include the URL for your database there:

Firebase.initializeApp(
    options: const FirebaseOptions(
        apiKey: "apikey",
        authDomain: "authDomain",
        databaseURL: "the URL for your database", // 👈
        projectId: "projectId",
        storageBucket: "storageBucket",
        messagingSenderId: "messagingSenderId",
        appId: "appId"));

You can find this URL in your browser by going to the Realtime Database panel in the Firebase console:

enter image description here


I'm not sure why the databaseURL doesn't show in your auto-complete though, as I have it in all of my Dart configs (most of which are generated by the FlutterFire CLI).

If entering the databaseURL manually gives an error message, I recommend checking if you're on the latest version of the libraries (although I don't recall this ever not being present).

Comments

-1

I recreated the flutter and firebase project with a new app id, and it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.