title: Flutter Redirect to a page on initState
body: <p>I have an application where you need to log in to continue (for example with Google).</p>
  
  <p>I would like to redirect the user when the authentification is needed.</p>
  
  <p>However when I run a <code>Navigator.of(context).pushNamed("myroute")</code>. I got the following error:</p>
  
  <pre><code> ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
  I/flutter ( 5624): The following assertion was thrown building _ModalScopeStatus(active):
  I/flutter ( 5624): setState() or markNeedsBuild() called during build.
  I/flutter ( 5624): This Overlay widget cannot be marked as needing to build because the framework is already in the
  I/flutter ( 5624): process of building widgets. A widget can be marked as needing to be built during the build phase
  I/flutter ( 5624): only if one of its ancestors is currently building. This exception is allowed because the framework
  I/flutter ( 5624): builds parent widgets before children, which means a dirty descendant will always be built.
  I/flutter ( 5624): Otherwise, the framework might not visit this widget during this build phase.
  </code></pre>
  
  <p>Here is a sample code</p>
  
  <pre><code>void main() {
      runApp(new MyApp());
  }
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return new MaterialApp(
        title: 'Flutter Demo',
        theme: new ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: new MyHomePage(title: 'Flutter Demo Home Page'),
        routes: &lt;String, WidgetBuilder&gt; {
          "login" : (BuildContext context) =&gt; new LoginPage(),
        }
      );
    }
  }
  
  class MyHomePage extends StatefulWidget {
    MyHomePage({Key key, this.title}) : super(key: key);
    final String title;
  
    @override
    _MyHomePageState createState() =&gt; new _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
  
    int _counter = 0;
  
      @override
      void initState() {
        super.initState();
  
        if(!isLoggedIn) {
          print("not logged in, going to login page");
          Navigator.of(context).pushNamed("login");
        }
  
      }
  
  
    void _incrementCounter() {
      setState(() {
        _counter++;
      });
    }
  
    void test() {
      print("hello");
    }
  
    @override
    Widget build(BuildContext context) {
  
      return new Scaffold(
        appBar: new AppBar(
          title: new Text(widget.title),
        ),
        body: new Center(
          child: new Text(
            'Button tapped $_counter time${ _counter == 1 ? '' : 's' }.',
          ),
        ),
        floatingActionButton: new FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: new Icon(Icons.add),
        ), // This trailing comma makes auto-formatting nicer for build methods.
      );
    }
  
  }
  
  class LoginPage extends StatefulWidget {
    LoginPage({Key key, this.title}) : super(key: key);
  
    final String title;
  
    @override
    _LoginPageState createState() =&gt; new _LoginPageState();
  }
  
  class _LoginPageState extends State&lt;LoginPage&gt; {
    @override
    Widget build(BuildContext context) {
      print("building login page");
      return new Scaffold(
        appBar: new AppBar(
          title: new Text("Sign up / Log In"),
        ),
        ),
      );
    }
  }
  </code></pre>
  
  <p>I guess I am doing something wrong, maybe aborting a widget build is causing this. However,, how can I achieve this.</p>
  
  <p>Basically : "I go on my page, if not logged, go to Login page"</p>
  
  <p>Thanks, 
  Alexi</p>