Flutter: being sent back to initial page after hot reload

1,008

I accidentally fixed this issue after a week of Googling desperately. I think it quite dumb that such an important config is not documented on the homepage.

class AppWidget extends StatelessWidget {
  // final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: "/",
      onGenerateRoute: Modular.generateRoute,
      navigatorKey: Modular.navigatorKey, // Here's the culprit
    );
  }
}

By using flutter_modular, the user has to put the Modular.navigatorKey into MaterialApp instead of generating a new one.

I am creating this question so no one has to go through all of my sufferings again.

Share:
1,008
Lê Quang Bảo
Author by

Lê Quang Bảo

Updated on December 19, 2022

Comments

  • Lê Quang Bảo
    Lê Quang Bảo over 1 year

    I am using flutter_modular to separate my app into some module, everything looks fine until I notice that each time I perform a hot reload, my application automatically jump back to login page which is also the initial one.

    This is my setting:

    class AppWidget extends StatelessWidget {
      final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          initialRoute: "/",
          onGenerateRoute: Modular.generateRoute,
          navigatorKey: navigatorKey,
        );
      }
    }
    

    Obviously, this issue does not happen to flutter native routing, so why does this appear in such a high voting package?

    Here the link to the dependency: https://github.com/Flutterando/modular

    And the link on pub.dev: https://pub.dev/packages/flutter_modular