1

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
j-vasil
  • 139
  • 1
  • 9
  • 1
    There's nothing wrong with what you're doing. Only improvement is that you can remove `BuildContext context` as a parameter for `_build`. However, I don't believe this is really on-topic here at SO. If you aren't having any problems with your code, I can't see your question being about anything other than code style, which is opinion-based and off-topic on SO. Questions should be about specific issues with your code. – Christopher Moore Apr 25 '21 at 03:58
  • @ChristopherMoore Thanks for the feedback. For the record, my intent wasn't to ask if this style was better than some other style but rather whether this really is only a style question or if there are subtleties in using FutureBuilder that make this inappropriate. I'm glad to know I've not opened my app up to a big issue that would pop up when I deploy to a real server. – j-vasil Apr 25 '21 at 04:18

0 Answers0