title: Flutter - Pass values between routes using pushReplacementNamed
body: <p>I'm trying to pass values between routes using <code>Navigator.pushReplacementNamed()</code>, but I'm not getting it.</p>
  
  <p>I can not figure out what the correct syntax and logic is in which data will exit <code>pushReplacementNamed</code> and get the <code>routes</code> key.</p>
  
  <p>Can you help me?</p>
  
  <p>Enter the code below to facilitate understanding:</p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(new MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return new MaterialApp(
        theme: new ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: new MyHomePage(),
        routes: &lt;String, WidgetBuilder&gt; {
          '/form': (BuildContext context) =&gt; new FormPage(email: ???, header: {'auth': ???}),
        },
      );
    }
  }
  
  class MyHomePage extends StatefulWidget {
    @override
    _MyHomePageState createState() =&gt; new _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        floatingActionButton: new FloatingActionButton(
          onPressed: () {
            Navigator.pushReplacementNamed(context, '/form', {email: 'myemail@flutter.com', header: {'auth': '1234'}});
          },
          child: new Icon(Icons.navigate_next),
        ),
      );
    }
  }
  
  class FormPage extends StatefulWidget {
    FormPage({Key key, this.email, this.header}) : super(key: key);
    String email;
    Map header;
    @override
    FormPageState createState() =&gt; new FormPageState();
  }
  
  class FormPageState extends State&lt;FormPage&gt; {
    @override
    Widget build(BuildContext context) {
      return new Container(
        child: new Column(
          children: &lt;Widget&gt;[
            new Text(widget.email),
            new Text(widget.header['auth'])
          ],
        ),
      );
    }
  }
  </code></pre>
  
  <p><strong>After correction:</strong></p>
  
  <p>The purpose of trying to use <code>pushReplacementNamed</code> was to remove the route history, so if the user presses the device's back button to get back to the earlier route, the route would no longer exist. <a href="https://stackoverflow.com/questions/44978216/flutter-remove-back-button-on-appbar">Reference</a></p>
  
  <p>The reason for this implementation is to be used on a login page, if the user has already been authenticated by <code>google sign</code> he is redirected to the next page passing login parameters and could not return to the login page by device's back button.</p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(new MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return new MaterialApp(
        theme: new ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: new MyHomePage(),
        routes: &lt;String, WidgetBuilder&gt; {
          '/form': (BuildContext context) =&gt; new FormPage(), //new
        },
      );
    }
  }
  
  class MyHomePage extends StatefulWidget {
    @override
    _MyHomePageState createState() =&gt; new _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        floatingActionButton: new FloatingActionButton(
          onPressed: () {
            Navigator.of(context).pushReplacement(                                                         //new
              new MaterialPageRoute(                                                                       //new
                settings: const RouteSettings(name: '/form'),                                              //new
                builder: (context) =&gt; new FormPage(email: 'myemail@flutter.com', header: {'auth': '1234'}) //new
              )                                                                                            //new
            );                                                                                             //new
          },
          child: new Icon(Icons.navigate_next),
        ),
      );
    }
  }
  
  class FormPage extends StatefulWidget {
    FormPage({Key key, this.email, this.header}) : super(key: key);
    String email;
    Map header;
    @override
    FormPageState createState() =&gt; new FormPageState();
  }
  
  class FormPageState extends State&lt;FormPage&gt; {
    @override
    Widget build(BuildContext context) {
      return new Container(
        child: new Column(
          children: &lt;Widget&gt;[
            new Text(widget.email),
            new Text(widget.header['auth'])
          ],
        ),
      );
    }
  }
  </code></pre>