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):
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()
localhost to your machine ip, in both firebase command line and your app. Some simulators, such as ios simulator, cannot understand localhost.await Firebase.initializeApp(); inside main function, not initStateIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With