title: Can Flutter Router Navigator and Provider at top level work together?
body: <p>I have a big problem using state management with Providers and the navigator.</p>
  
  <p>Things that we must know:</p>
  
  <p>1) My RESTFUL API gives a token that expires on 12h.</p>
  
  <p>2) My home: parameter is conditioned if the user is authenticated or nor.. if not, based in other parameters I must decide what screen must be the home.</p>
  
  <p>3) If I'm on one screen and the token gets expired on ANY request that the app is doing to the API I:</p>
  
  <ul>
  <li>Call a function inside my <code>auth</code> provider that do logout procedures, (i clean the localStorage with the token). This <code>auth</code> provider sets a property called <code>isAuth</code> to <code>false</code>. </li>
  <li>When <code>isAuth</code> is false, if you look the <code>main.dart</code> file at <code>home:</code> you will see that will show the <code>LoginPage()</code>.</li>
  </ul>
  
  <p>Everything here is good. The problem is when i must do a redirect using the <code>Navigator.of...</code>.. If, for example, in case of error of token expired I do: <code>Navigator.popUntil(context, ModalRoute.withName('/login'));</code> the app goes to the <code>LoginPage()</code> without problem, but... when I try to use the login (using the <code>login</code> function of <code>auth</code> provider) it's stay on the login page! If I use in any place the <code>Navigator</code> to go to any other place, it's appears to override the 'state management way' that I use to control which page shows as <code>home</code>. </p>
  
  <p>Can you help me please?</p>
  
  <p>My <code>main.dart</code> code:</p>
  
  <pre><code>// Providers
  import 'package:provider/provider.dart';
  import 'package:cadsanjuan_movil/providers/auth.dart';
  
  // Pages
  import './pages/login_page.dart';
  import './pages/initial_loading_page.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    final storage = new FlutterSecureStorage();
    @override
    Widget build(BuildContext context) {
      return MultiProvider(
          providers: [
            ChangeNotifierProvider&lt;Auth&gt;(create: (context) =&gt; Auth()),
            ChangeNotifierProvider&lt;TurnActive&gt;(create: (context) =&gt; TurnActive()),
            ChangeNotifierProxyProvider&lt;TurnActive, Checkup&gt;(
                create: (context) =&gt; Checkup(),
                update: (_, turnActive, checkup) {
                  checkup.turnData = turnActive.turnData;
                  return checkup;
                }),
            ChangeNotifierProxyProvider&lt;Auth, ErrorProvider&gt;(
                create: (context) =&gt; ErrorProvider(),
                update: (_, auth, errorProvider) {
                  errorProvider.logoff = auth.logoff;
                  return errorProvider;
                }),
          ],
          child: Consumer3&lt;Auth, TurnActive, Checkup&gt;(
            builder: (context, auth, turnActive, checkup, _) {
              return MaterialApp(
                title: 'CAD App',
                theme: ThemeData(
                  primarySwatch: Colors.blue,
                ),
                home: auth.isAuth
                    ? (turnActive.turnData != null &amp;&amp;
                            turnActive.turnData['turn']['checkUpStartId'] != null)
                        ? CheckupPage() // Si el cotejo está en proceso
                        : InitialLoadingPage() // Si no hay cotejo iniciado
                    : FutureBuilder(
                        // Si estamos no hay autenticación en el estado, se chequea en el localStorage
                        future: auth.tryAutoLogin(),
                        builder: (ctx, authResultSnapshot) =&gt;
                            authResultSnapshot.connectionState ==
                                    ConnectionState.waiting
                                ? LoadingData()
                                : LoginPage(),
                      ),
                routes: {
                  LoginPage.routeName: (context) =&gt; LoginPage(),
                  InitialLoadingPage.routeName: (context) =&gt; InitialLoadingPage(),
                  CheckupPage.routeName: (context) =&gt; CheckupPage(),
                  CheckupGroupsPage.routeName: (context) =&gt; CheckupGroupsPage(),
                },
              );
            },
          ));
    }
  }
  </code></pre>