Good usage for FutureBuilder of flutterfire?

Hi,

I’m new to flutter and I wanna know if it’s a good pattern for initialize app with flutterfire ?

My code :

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(FlashChat());
}

class FlashChat extends StatelessWidget {
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      // Initialize FlutterFire:
      future: _initialization,
      builder: (context, snapshot) {
        // Check for errors
        if (snapshot.hasError) {
          print('Something went wrong in Flutter Fire');
        }

        // Once complete, show your application
        if (snapshot.connectionState == ConnectionState.done) {
          return MaterialApp(
            initialRoute: WelcomeScreen.routeName,
            routes: {
              WelcomeScreen.routeName: (context) => WelcomeScreen(),
              LoginScreen.routeName: (context) => LoginScreen(),
              RegistrationScreen.routeName: (context) => RegistrationScreen(),
              ChatScreen.routeName: (context) => ChatScreen(),
            },
          );
        }

        // Otherwise, show something whilst waiting for initialization to complete
        return Container(
          color: Colors.white,
          child: Center(
            child: CircularProgressIndicator(),
          ),
        );
      },
    );
  }
}

Have a nice day :slight_smile:

Hey @jbty it is good practice to do it this way as the oficial docs state. But in my case, for example, I had a blank screen appearing always when snapshot.connectionState changed to done. Plus I would rather prefer to keep the splash screen until Firebase is initialize.

For that you can check an issue I opened when they updated the Firebase packages and the solution. For me it is the best way to init Firebase.

Solution Firebase init

If you have any questions let me know :wink:

1 Like

@F.lucadetena Thanks for your ask ^^