0

I would like to use a Firebase Database in Flutter, but while the authentification is working, I get an error for the Firebase Database .

I am initializing the app with

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // I also found this version: FirebaseDatabase database = FirebaseDatabase.instance;
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform
  );
  
  FirebaseFirestore.instance.settings = const Settings(
    persistenceEnabled: true,
  );


  FirebaseUIAuth.configureProviders([
    EmailAuthProvider(),
    ...
  ]);

  runApp(
    ChangeNotifierProvider(
          create : (context) => MyState(),
          child : MaterialApp(
              ...
              home: MainPage()),
        )
    );
}

and following this tutorial, I would like to read and write the database in any widget by initializing

DatabaseReference ref = FirebaseDatabase.instance.ref("users/123");

and using it in the build method like

await ref.set({
  "name": "John",
  "age": 18,
  "address": {
    "line1": "100 Mountain View"
  }
});

but I get the error

The following NativeError object was thrown building SomeWidget(state: _SomeWidgetState#87613):
Error: FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://.firebaseio.com

Where do you find this url, where do you initialize this in the app?


  • firebase_database: ^11.3.5
  • cloud_firestore: ^5.6.5
  • cloud_firestore: ^5.6.5
3
  • 1
    are you using the latest version of firebase_database? Commented May 15 at 13:52
  • How did you initialize Firebase in this project? Did you run the flutterfire CLI as shown here:firebase.google.com/docs/flutter/…? --- Also: note that Firestore and Firebase (Realtime) Database are completely separate products. While both part of Firebase, they are completely separate and the SDKs and APIs for one, won't work with the other. Your code tries to use the Realtime Database, not Firestore - so if you're actually trying to use Firestore, you'll want to reread that documentation for code samples. Commented May 15 at 14:33
  • see this may be the reason error-firebase-fatal-error-cannot-parse-firebase-url-please-use-https-your Commented May 15 at 14:34

1 Answer 1

1

Given how you call initializeApp the Firebase SDK will try to read the URL for the Realtime Database (which is what you use when you depends on firebase_database and access FirebaseDatabase API calls) from the firebase_options.dart file that is automatically generated when you run the Firebase/FlutterFire CLI tools to initialize things.

Since you get an error message, it seems that you either didn't run those tools or that you ran them before you enabled the Realtime Database in the Firebase console for your project. If either of those is the cause, you can fix it by running flutterfire config (again) to update the local configuration files/code.


If the above doesn't work, you can consider initializing the Realtime Database with its URL in code. To do that, copy your database URL from the Firebase console, and add it to your code here:

DatabaseReference ref = FirebaseDatabase.instanceFor(
   Firebase.app(),
   "your database URL"
).ref("users/123");

As said though, I'd only recommend doing this if the CLI approach doesn't work - or if you have special requirements.


Also see:

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

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.