title: Save state of a widget
body: <p>I have a button that pushes a page where user fills some data and they can then exit the page but come back and edit it. </p>
  
  <p>I provided a simplified example, the goal is to be able to maintain the counter of "count page" after exiting it without having to save the counter in some other variable. I want a general way to save the state of a widget.</p>
  
  <p>So in other words I don't want the state disposed, I want to save that so when I navigate back to that page it's like I never popped it. </p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Save State',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: HomePage(),
      );
    }
  }
  
  class HomePage extends StatefulWidget {
    @override
    _HomePageState createState() =&gt; _HomePageState();
  }
  
  class _HomePageState extends State&lt;HomePage&gt; {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child: RaisedButton(
            child: Text("Go to counter page"),
            onPressed: () {
              Navigator.push(context,
                  MaterialPageRoute(builder: (context) =&gt; CounterPage()));
            },
          ),
        ),
      );
    }
  }
  
  class CounterPage extends StatefulWidget {
    @override
    _CounterPageState createState() =&gt; _CounterPageState();
  }
  
  class _CounterPageState extends State&lt;CounterPage&gt; {
    int _counter = 0;
  
    void _incrementCounter() {
      setState(() {
        _counter++;
      });
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: &lt;Widget&gt;[
              Text(
                'You have pushed the button this many times:',
              ),
              Text(
                '$_counter',
                style: Theme.of(context).textTheme.headline4,
              ),
              RaisedButton(
                child: Text("Go back"),
                onPressed: () {
                  Navigator.pop(context);
                },
              )
            ],
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _incrementCounter,
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),
      );
    }
  }
  
  </code></pre>