I'd created a MyApp class that was moderately complex (for me as a new flutter user)--
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: appName, // Browser tab name JAV
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: WelcomeScreen.id,
routes: {
'/myHomePage': (context) => MyHomePage( // to be deleted soon
title: 'my home page'
),
//ChatScreen.id: (context) => ChatScreen(),
LoginScreen.id: (context) => LoginScreen(),
NotImplementedScreen.id: (context) => NotImplementedScreen(),
RegisterScreen.id: (context) => RegisterScreen(),
//SignUpScreen.id: (context) => SignUpScreen(),
WelcomeScreen.id: (context) => WelcomeScreen(), // == default route == initial route.
}, // routes
debugShowCheckedModeBanner: false, // Remove the 'DEBUG' banner in the top right corner.
);
} // _build
} // class
--when I decided I wanted to use some FlutterFire (aka Firebase for Flutter) code. I was bothered by how confusing the code was going to look once I added the FutureBuilder (or StatefulWidget) code to properly initialize Firebase (per https://firebase.flutter.dev/docs/overview).
Then I realized that I could keep the code clean by renaming my existing build method to _build and having FutureBuilder return it when the initialization was complete [I think this idea is sometimes called Copelien's Envelope-Letter idion?]. I gave this a try and it SEEMS to work.
I'll test more tomorrow but I'd really love to hear opinions from some experienced Flutter/Firebase developers about whether there are problems with this approach that aren't obvious to a newbie. The implementation is shown below. Thanks in advance for your time!
class MyApp extends StatelessWidget {
// Create the initialization Future outside of `build`:
final Future<FirebaseApp> _initialization = Firebase.initializeApp();
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return FutureBuilder(
// Initialize FutterFire:
future: _initialization,
builder: (context, snapshot) {
// Check for errors
if (snapshot.hasError) {
String err = 'Something went wrong! Firebase initialization error: ${snapshot.error.toString()}';
print (err);
return Text(err);
}
// Once complete, show your application
if (snapshot.connectionState == ConnectionState.done) {
return _build(context); // <<<<<<<<< Call previous version of build here!
}
// Otherwise, show something whilst waiting for initialization to complete
return Center(
child: CircularProgressIndicator(),
);
},
); // FutureBuilder
} // build
// This is the build method from before adding Firebase.
Widget _build(BuildContext context) {
return MaterialApp(
title: appName, // Browser tab name JAV
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: WelcomeScreen.id,
routes: {
'/myHomePage': (context) => MyHomePage( // to be deleted soon
title: 'my home page'
),
//ChatScreen.id: (context) => ChatScreen(),
LoginScreen.id: (context) => LoginScreen(),
NotImplementedScreen.id: (context) => NotImplementedScreen(),
RegisterScreen.id: (context) => RegisterScreen(),
//SignUpScreen.id: (context) => SignUpScreen(),
WelcomeScreen.id: (context) => WelcomeScreen(), // == default route == initial route.
}, // routes
debugShowCheckedModeBanner: false, // Remove the 'DEBUG' banner in the top right corner.
);
} // _build
} // class