Flutter 2.10: hot reload displaying blank screen

Viewed 127

I upgraded to flutter 2.10 for a new projet, I setup my project like i usually do (injectable, getIt... ect). what I'm facing right now, is whenever I do a hot reload (ctrl+s), the whole application rebuilds but it displays normally for a fraction of a second then it displays a blank screen. like so:

also when looking at the inspector after the unexpected blank screen I only find:

enter image description here

knowing that normally it displays the widget's tree (login screen...).

the main function:

void main() async {
  setupLogging();
  setPathUrlStrategy();
  final LocalizationDelegate delegate = await createDelegate();
  BlocOverrides.runZoned(
    () => runApp(LocalizedApp(delegate, Application())),
    blocObserver: ApplicationObserver(),
  );
}

the Application Class:

class Application extends StatelessWidget {
  Application({Key? key}) : super(key: key);

  final initializer = initialize();

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: initializer,
        builder: (context, snapshot) {
          switch (snapshot.connectionState) {
            case ConnectionState.none:
            case ConnectionState.waiting:
            case ConnectionState.active:
              return Container(
                child: const Center(
                  child: CircularProgressIndicator(),
                ),
              );
            case ConnectionState.done:
              log("**************************** or here");
              final delegate = getDelegate(context);
              final AppRouter router = AppRouter(authGuard: getIt<AuthGuard>());
              return LocalizationProvider(
                state: LocalizationProvider.of(context).state,
                child: MaterialApp.router(
                  routeInformationParser: router.defaultRouteParser(),
                  routerDelegate: router.delegate(),
                  localizationsDelegates: [
                    GlobalMaterialLocalizations.delegate,
                    GlobalWidgetsLocalizations.delegate,
                    GlobalCupertinoLocalizations.delegate,
                    delegate,
                  ],
                  supportedLocales: delegate.supportedLocales,
                  locale: delegate.currentLocale,
                  theme: ApplicationTheme.lightTheme,
                  // darkTheme: ApplicationTheme.darkTheme , todo
                ),
              );
          }
        });
  }

  static Future initialize() async {
    await configureDependencies();
  }
}

the logs after hot reload display:

[log] **************************** or here  // at the application state done
[log] ****************************** inside the login? // at the login screen build
[log] **************************** or here  // back at the application state done, but no 
login screen after this...

What may be the cause of thise issue? any hints please?

1 Answers

The issue is not related to flutter 2.10, it is because of where i'm creating the application router. since i'm creating the application router inside the FutureBuilder, each time i hot reload, a new router gets created and weird things happen.

I fixed it by delegating the creation of my router to getIt as a singleton, and getit after the FutureBuilder is done, since it's a singleton, it won't be recreated each time.

final AppRouter router = getIt<AppRouter>();
Related