Flutter: being sent back to initial page after hot reload
Asked Answered
M

1

6

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

Manna answered 30/3, 2020 at 9:51 Comment(0)
M
9

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.

Manna answered 30/3, 2020 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.