title: How to use Navigator.popUntil Flutter
body: <p>I m doing a Flutter app and I would like to go back from page 4 to page 1. 
  I have an error really strange :</p>
  
  <blockquote>
    <p>Bad state : Future already completed</p>
  </blockquote>
  
  <p>I created a simple project to reproduce this bug :</p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Flutter Demo',
        onGenerateRoute: routes,
      );
    }
  }
  
  Route routes(RouteSettings settings) {
    if (settings.name == '/page1') {
      return MaterialPageRoute(
        builder: (context) {
          return Page1();
        },
      );
    } else if (settings.name == '/page2') {
      return MaterialPageRoute(
        builder: (context) {
          return Page2();
        },
      );
    } else if (settings.name == '/page3') {
      return MaterialPageRoute(
        builder: (context) {
          return Page3();
        },
      );
    } else if (settings.name == '/page4') {
      return MaterialPageRoute(
        builder: (context) {
          return Page4();
        },
      );
    } else {
      return MaterialPageRoute(
        builder: (context) {
          return Page1();
        },
      );
    }
  }
  
  class Page1 extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Text('Page 1'),
              RaisedButton(
                child: Text('Go Page 2'),
                onPressed: () {
                  Navigator.of(context).pushNamed('/page2');
                },
              )
            ],
          ),
        ),
      );
    }
  }
  
  class Page2 extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Text('Page 2'),
              RaisedButton(
                child: Text('Go Page 3'),
                onPressed: () {
                  Navigator.of(context).pushNamed('/page3');
                },
              )
            ],
          ),
        ),
      );
    }
  }
  
  class Page3 extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Text('Page 3'),
              RaisedButton(
                child: Text('Go Page 4'),
                onPressed: () {
                  Navigator.of(context).pushNamed('/page4');
                },
              )
            ],
          ),
        ),
      );
    }
  }
  
  class Page4 extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Text('Page 4'),
              RaisedButton(
                child: Text('Go Back Page 1'),
                onPressed: () {
                  Navigator.of(context).popUntil(ModalRoute.withName('/page1'));
                },
              )
            ],
          ),
        ),
      );
    }
  }
  </code></pre>
  
  <p>How can I solve that ?</p>