2

My question is how to show a splash screen or alternatively show a loading screen if I execute the following code:

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MyApp());
}
  • Your title says that `initializeApp` is taking a lot of time, which it shouldn't. Can you show some measurements of how long it takes? – Frank van Puffelen Apr 09 '21 at 15:07
  • 1
    What @FrankvanPuffelen said.. and then if you want to show splash screen there are numerous Q&As here on SO. This is one of many: https://stackoverflow.com/questions/43879103/adding-a-splash-screen-to-flutter-apps – Robert Sandberg Apr 09 '21 at 15:13

1 Answers1

3

You can simply use Future Builder for that,

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  //remove this line from your code
  //await Firebase.initializeApp();
  runApp(MyApp());
}

this is code for MyApp()

class MyApp extends StatelessWidget {
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: FutureBuilder(
        future: _initialization,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return LoginWrapper();
          }
          return SplashScreen();
        },
      ),
    );
  }
}

it shows your SplashScreen while loading your firebase App ;)

Anushka Pubudu
  • 389
  • 3
  • 10