title: flutter stateful widget lose data on hot reload and after pushNamed
body: <p>I'm not sure what to look up for so feel free to give me pointers as to where to look also. I'm learning so this is definitely appreciated.</p>
  
  <p>I've just discovered that I can use named routes in flutter and I'm trying to move to thing to make the behavior of my app more predictable. However, at some point, my UserDataContainer Stateful Widget loses his state values. So I have some user data and it disappears.</p>
  
  <p>Here is some of the code.</p>
  
  <p>Here is my user data container:</p>
  
  <pre><code>// app_state_container.dart
  //import 'package:advanced_app/models/app_state.dart';
  import 'package:flutter/foundation.dart';
  import 'package:flutter/material.dart';
  import 'package:myapp/FoodLib/user.dart';
  
  class UserDataContainer extends StatefulWidget {
  // Your apps state is managed by the container
    final UserDataType userdata = UserDataType();
  // This widget is simply the root of the tree,
  // so it has to have a child!
    final Widget child;
  
    UserDataContainer({@required this.child
  //    @required this.userdata,
        }) {
      print("CREATED USERDATA CONTAINER");
      print("The name: " + this.userdata.name);
    }
  
  // This creates a method on the AppState that's just like 'of'
  // On MediaQueries, Theme, etc
  // This is the secret to accessing your AppState all over your app
    static _UserDataContainerState of(BuildContext context) {
      return (context.inheritFromWidgetOfExactType(_InheritedUserDataContainer)
              as _InheritedUserDataContainer)
          .data;
    }
  
    @override
    _UserDataContainerState createState() =&gt; new _UserDataContainerState();
  }
  
  class _UserDataContainerState extends State&lt;UserDataContainer&gt; {
  // Just padding the state through so we don't have to
  // manipulate it with widget.state.
  //  UserDataType userdata  = UserDataType();
  
    String name = "test";
  
    @override
    void initState() {
  // You'll almost certainly want to do some logic
  // in InitState of your AppStateContainer. In this example, we'll eventually
  // write the methods to check the local state
  // for existing users and all that.
      super.initState();
    }
  
  // So the WidgetTree is actually
  // AppStateContainer --&gt; InheritedStateContainer --&gt; The rest of your app.
    @override
    Widget build(BuildContext context) {
      return new _InheritedUserDataContainer(
        data: this,
        child: widget.child,
      );
    }
  }
  
  // This is likely all your InheritedWidget will ever need.
  class _InheritedUserDataContainer extends InheritedWidget {
  // The data is whatever this widget is passing down.
    final _UserDataContainerState data;
  
  // InheritedWidgets are always just wrappers.
  // So there has to be a child,
  // Although Flutter just knows to build the Widget thats passed to it
  // So you don't have have a build method or anything.
    _InheritedUserDataContainer({
      Key key,
      @required this.data,
      @required Widget child,
    }) : super(key: key, child: child);
  
  // This is a better way to do this, which you'll see later.
  // But basically, Flutter automatically calls this method when any data
  // in this widget is changed.
  // You can use this method to make sure that flutter actually should
  // repaint the tree, or do nothing.
  // It helps with performance.
    @override
    bool updateShouldNotify(_InheritedUserDataContainer old){
      if(
      data.widget.userdata.fooditems.length !=
          old.data.widget.userdata.fooditems.length){
        return true;
  
      }
  //    print("SAME");
      return false;
    }
  }
  </code></pre>
  
  <p>Where <code>UserDataType</code> is the class that contains the data that diseappear.</p>
  
  <p>This is my main app with the Material class:</p>
  
  <pre><code>class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return new DynamicTheme(
          defaultBrightness: Brightness.light,
          data: (brightness) =&gt; new ThemeData(
                primarySwatch: Colors.teal,
                brightness: brightness,
              ),
          themedWidgetBuilder: (context, theme) {
            return UserDataContainer(
              child: MaterialApp(
                title: 'Title',
                home: UserLoader(),
                theme: theme,
  
                routes: {
                  // When navigating to the "/" route, build the FirstScreen widget.
                  '/userpage': (context) =&gt; User(),
                  '/settings': (context) =&gt; SettingsPage(),
                  '/saved_stuff' : (context) =&gt; SavedRecipesPage(),
                  '/database' : (context) =&gt; Database(),
                  '/new_list' : (context) =&gt; ShoppingKartPage(),
                  '/stats' : (context) =&gt; StatPage(),
                  // When navigating to the "/second" route, build the SecondScreen widget.
  //                '/second': (context) =&gt; SecondScreen(),
                },
  
              ),
            );
          });
    }
  }
  
  class UserLoader extends StatelessWidget {
    @override
  
  
  
  
    Widget build(BuildContext context) {
      print("Version 1");
  
      final ThemeData theme = Theme.of(context);
  
      return FutureBuilder&lt;void&gt;(
          future: UserDataContainer.of(context).widget.userdata.init(),
          builder: (BuildContext context, AsyncSnapshot&lt;void&gt; snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              print("Build user");
  
  
  
  
              return User();
            } else {
              return SplashScreen();
            }
          });
    }
  }
  
  class SplashScreen extends StatefulWidget {
    @override
    _SplashScreenState createState() =&gt; new _SplashScreenState();
  }
  
  class _SplashScreenState extends State&lt;SplashScreen&gt; {
  
    @override
    void initState() {
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        body: new Center(
          child: new Image.asset('images/icon.png'),
        ),
      );
    }
  }
  </code></pre>
  
  <p>The material app has my routes and the UserLoader is there so I can load some User data saved on the phone at startup. It puts a splash screen and wait until the data is loaded.</p>
  
  <p>In my drawer I've replaced the code to move to specific page by the named version:</p>
  
  <pre><code>Navigator.push(
        context,
        MaterialPageRoute(
            builder: (context) =&gt; SettingsPage()));
  </code></pre>
  
  <p>Became</p>
  
  <pre><code>Navigator.of(context).pop();
  </code></pre>
  
  <p>For example.</p>
  
  <p>However, now when I'm using the app, the UserDataContainer sometimes contains a totally new instance of User() (which is a dart class, not a widget) and I have no idea why the app suddeny behaves like this.</p>
  
  <p>Edit:</p>
  
  <p>I tried removing the FutureBuilder by using this instead of my futureloader but the state of the UserContainer get rebuild everytime (without being init again though):</p>
  
  <pre><code>class LoadingScreen extends StatefulWidget {
    @override
    _LoadingScreenState createState() =&gt; new _LoadingScreenState();
  
  }
  
  class _LoadingScreenState extends State&lt;LoadingScreen&gt;{
  
    @override
    void initState() {
      super.initState();
  
      SchedulerBinding.instance.addPostFrameCallback((_){
        runInitTasks();
      });
  
  
    }
  
    @protected
    Future runInitTasks() async {
      await UserDataContainer.of(context).widget.userdata.init();
  
      Navigator.of(context).pushReplacementNamed('/fridge');
    }
  
    @override
    Widget build(BuildContext context){
      return Scaffold(
        body: new Center(
          child: new Image.asset('images/icon.png'),
        ),
      );
    }
  }
  </code></pre>