Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: being sent back to initial page after hot reload

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

like image 954
Lê Quang Bảo Avatar asked Mar 30 '20 09:03

Lê Quang Bảo


1 Answers

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.

like image 82
Lê Quang Bảo Avatar answered Oct 26 '22 23:10

Lê Quang Bảo