7

flutterfire recently introduced some changes and one of those is the need to call initializeApp() before using any plugin.

is it ok to call it in the main ? something like this :

void main() async {
  await Firebase.initializeApp();
  return runApp(App());
}

or maybe like this without async await

void main() {
  Firebase.initializeApp();
  return runApp(App());
}

i think the first example is more correct but its blocking the execution of the main and i have no idea how much time before the future complete?

xnio94
  • 91
  • 2
  • 7
  • Check the dup for different ways to call it, calling it in main won't affect anything just matter of preference. – Peter Haddad Aug 30 '20 at 05:11
  • what do you mean by "dub" ? – xnio94 Aug 30 '20 at 16:48
  • 1
    duplicate https://stackoverflow.com/questions/63492211/no-firebase-app-default-has-been-created-call-firebase-initializeapp-in – Peter Haddad Aug 30 '20 at 16:49
  • thank you @Peter, i already read your answer, my concerns were about the time it will take before the future complete, its working fine on my device, but i wasn't sure if there will be some situations where it will take too long, which means the app won't start, (not a great user experience), but as frank explained the initialization will just wait to the call to native code to complete, so I think it's okay. – xnio94 Aug 30 '20 at 18:10

1 Answers1

15

The only requirement from Firebase is that you call Firebase.initializeApp() before any other Firebase APIs are called. If that condition isn't met, it will raise an exception explicitly telling you so.

In my code I call it in my main, which is the earliest place I can think if, and it works without problems. If you're using the google-services.json or GoogleServices-Info.plist files to store the configuration data, the code is

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  ...

If you use the pure Dart initialization from the documentation on initializing Firebase, the equivalent would be:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  ...

The initialization of Firebase at this level is pretty much instantaneous, as it's just waiting for the call to the native code to complete, and the native code itself does nothing more then look up the configuration values.

But if that is taking too long for you, you can call it without await. It just means that you may have to deal with that Future<FirebaseApp> later in your code where you access Firebase, typically by wrapping that in a FutureBuilder.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thanks for the explanation it answered all my queries, i think you should also add `WidgetsFlutterBinding.ensureInitialized();` before calling it because otherwise, flutter will raise an exception – xnio94 Aug 30 '20 at 00:13
  • 1
    Darn... I have that in the app that I copied this from, but hoped it wouldn't be needed for you. Added it back now. :) – Frank van Puffelen Aug 30 '20 at 01:22
  • 1
    Thx that's it. Here is how to do it with the use of async instead of Future : `void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); runApp(const MyApp()); }` – Jack_Modesta Mar 26 '23 at 09:57