Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use FlutterFire with a emulator demo project?

I am using the firebase emulator with demo project, without any actual firebase project resource created. I am doing so with the following command...

firebase emulators:start --project demo-test --only firestore

This successfully starts a Firebase emulator instanceat localhost:8080.

Then I am connecting to the firebase in my flutter app as follows...

await Firebase.initializeApp();
FirebaseFirestore.instance.settings = const Settings(
  host: 'localhost:8080',
  sslEnabled: false,
  persistenceEnabled: false,
);

When initializeApp is run, I get the following error...

E/flutter ( 7724): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: [core/not-initialized] Firebase has not been correctly initialized.
E/flutter ( 7724):
E/flutter ( 7724): Usually this means you've attempted to use a Firebase service before calling `Firebase.initializeApp`.
E/flutter ( 7724):
E/flutter ( 7724): View the documentation for more information: https://firebase.flutter.dev/docs/overview#initialization
E/flutter ( 7724):
E/flutter ( 7724): #0      MethodChannelFirebase.initializeApp
package:firebase_core_platform_interface/…/method_channel/method_channel_firebase.dart:113
E/flutter ( 7724): <asynchronous suspension>
E/flutter ( 7724): #1      Firebase.initializeApp
package:firebase_core/src/firebase.dart:40
E/flutter ( 7724): <asynchronous suspension>
E/flutter ( 7724): #2      _MyHomePageState._asyncInit
package:mobile_app/main.dart:53
E/flutter ( 7724): <asynchronous suspension>
E/flutter ( 7724):
like image 318
Scorb Avatar asked Oct 25 '25 02:10

Scorb


2 Answers

My current app has AppConfig class that extends InheritedWidget that keeps instance available when in use.

You can try with

class AppConfig extends InheritedWidget {

    static AppConfig? of(BuildContext context) {
        initializeFirebase();
        return context.dependOnInheritedWidgetOfExactType<AppConfig>();
      }
    
    static Future<void> initializeFirebase() async {
        try {
          await Firebase.initializeApp();
        } catch (e) {}
      }
}

You need to initialise the AppConfig() in main()

like image 171
Mukund Jogi Avatar answered Oct 27 '25 14:10

Mukund Jogi


  1. Try to change localhost to your machine ip, in both firebase command line and your app. Some simulators, such as ios simulator, cannot understand localhost.
  2. Please paste a fully reproducible sample, instead of just part of the lines.
  3. Try to await Firebase.initializeApp(); inside main function, not initState
  4. Firstly check at port 8080 manually and see whether the firebase server really works
like image 44
ch271828n Avatar answered Oct 27 '25 14:10

ch271828n