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
.