title: Flutter - Always execute a function when the page appears
body: <p>How could I make the <code>name()</code> function run whenever the <code>Page1</code> page appeared?</p>
  
  <p>In the code below before going to <code>Page2</code> I execute the <code>dispose()</code>
  Already inside <code>Page2</code> if I click the back button or the physical button of Android the function <code>name()</code> is not executed, but if I click the <code>'go to Page1'</code> button the function <code>name()</code> is executed.</p>
  
  <p>Could you help me to always execute the <code>name()</code> function when <code>Page1</code> appears?</p>
  
  <p><a href="https://i.stack.imgur.com/eiHPv.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/eiHPv.gif" alt="enter image description here"></a></p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() {
    runApp(new MyApp());
  }
  
  class MyApp extends StatelessWidget {  
    @override
    Widget build(BuildContext context) {
      return new MaterialApp(
        home: new MyHomePage(),
        routes: &lt;String, WidgetBuilder&gt; {
          '/page2': (BuildContext context) =&gt; new Page2(),
        },
      );
    }
  }
  
  class MyHomePage extends StatefulWidget {
    @override
    _MyHomePageState createState() =&gt; new _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
    String nameScreen;
  
    String name() {
      return 'foo1';
    }
  
    @override
    void initState() {
      super.initState();
      this.nameScreen = name();
  
    }
  
    @override
    void dispose() {
      this.nameScreen = '';
      super.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          title: new Text('Page 1'),
          backgroundColor: new Color(0xFF26C6DA),
        ),
        body: new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              new RaisedButton(
                child: const Text('go to Page2'),
                onPressed: () async {
                  dispose();
                  bool isLoggedIn = await Navigator.of(context).pushNamed('/page2');
                  if (isLoggedIn) {
                    setState((){
                      this.nameScreen = name();
                    });
                  }
                },
              ),            
              new Text(
                '$nameScreen',              
              ),
            ],
          ),
        ),
      );
    }
  }
  
  class Page2 extends StatelessWidget{
    @override
    Widget build(BuildContext context) {
      return new Scaffold( 
        appBar: new AppBar(
          title: new Text('Page 2'),
          backgroundColor: new Color(0xFFE57373)
        ),
        body: new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              new RaisedButton(
                child: const Text('go back to Page1'),
                onPressed: () {
                  Navigator.pop(context, true);
                }
              ),
            ],
          ),
        ),
      );
    }
  }
  </code></pre>