title: How to get back to app home page after successful login in Flutter
body: <p>I'm trying to implement the user authentification into my Flutter app that is supposed to work for both Android and iOS.</p>
  <p>I am using the JWT for the authentification of the app's request to the server, so I provide the user with a JWT token at login and the app saves the token securely on the device and then uses the token to communicate with my server.</p>
  <p>When the app launches, I run the HomePage where there is a if statement where app looks to the secure storage for a JWT token if there is no token, the app shows the login page.</p>
  <p>HomePage class:</p>
  <pre><code>class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.orange,
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: MyHomePage(title: 'App Title'),
        routes: &lt;String, WidgetBuilder&gt;{
          '/login': (BuildContext context) =&gt; new SignInPage(),
        },
      );
    }
  }
  
  class MyHomePage extends StatefulWidget {
    MyHomePage({Key key, this.title}) : super(key: key);
    final String title;
  
    @override
    _MyHomePageState createState() =&gt; _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
    ...
    @override
    Widget build(BuildContext context) {
  
      storage.read(key: 'jwt_token').then((token) {
        logger.d('MAIN APP GOT TOKEN: ' + token.toString());
        if (token == null) {
          Navigator.pushReplacementNamed(context, '/login');
        }
      });
  
      return Scaffold(...);
    }
  }
  </code></pre>
  <p>I tried <code>Navigator.pushReplacementNamed</code>, <code>Navigator.pushNamed</code>, <code>Navigator.popAndPushNamed</code>... All of those get me to the login page. So that's fine. My problem is, that it doesn't work the other way around.</p>
  <p>When I login sucessfully (obtain a JWT token), I can't make the app to return to the home page. If I restart the application it correctly shows the home page (as now there is a JWT in the secure storage), so the token check works fine. It must be something in rerouting from the login page to the home page.</p>
  <p>login page snippet:</p>
  <pre><code>if (serverResponse.containsKey('jwt_token')) {
      await storage.write(
           key: 'jwt_token', value: serverResponse['jwt_token']);
      Navigator.pushReplacementNamed(context, '/');
  </code></pre>
  <p>I tried also <code> Navigator.pop</code> and  <code>Navigator.pushNamed(context, '/')</code>, <code>Navigator.pushNamedAndRemoveUntil(context, '/', (route) =&gt; false)</code>, <code>Navigator.pushReplacementNamed(context, '/')</code>, non of which worked.</p>
  <p>It looks like the page is changing, but it ends up being the login page again. I checked that the token is already inside the storage when the rerouting command is called. Also I have a debug message above the token check in the HomePage and the debug string is never printed after the login, so the Home page is simply not called at all.</p>
  <p>Do you see something wrong in my code? How can I make it work?</p>