title: Flutter reuse state from StatefulWidget on rebuild
body: <p>In short, the question is: <strong>How do I reuse the state of an entire widget subtree?</strong></p>
  <hr />
  <p>This is what my code currently looks like:</p>
  <pre><code>...
  BlocBuilder&lt;AuthBloc, AuthState&gt;(
    builder: (context, state) {
      if (state is Authenticating) {
        return AppLoading();
      } else if (state is NotAuthenticated) {
        return AppOnboarding();
      } else if (state is Authenticated) {
        return AppMain();
      } else {
        return null;
      }
    }
  ),
  ...
  </code></pre>
  <p>Nothing fancy here, just a BlocBuilder rebuilding its child whenever the state of the underlying Bloc changes.</p>
  <p>Now, take for instance the following state transitions: <code>NotAuthenticated =&gt; Authenticating =&gt; NotAuthenticated</code> because there was something wrong with the inputed information. This would result in the AppOnboarding() widget being rebuild completely from scratch with all the information lost. How can I reuse the state from the old AppOnboarding() widget to make it look like the widget was never rebuild?</p>
  <hr />
  <h1>What I've already tried</h1>
  <p>I've already tried using a <code>GlobalKey</code> for this which I would pass to the key property of my AppOnboarding widget. This is my code:</p>
  <pre><code>_AuthenticatingState extends State&lt;Authenticating&gt; {
    GlobalKey&lt;AppOnboardingState&gt; _appOnboardingKey;
  
    @override
    initState() {
      super.initState();
      _appOnboardingKey = GlobalKey();
    }
  
    @override
    Widget build(BuildContext context) {
      return BlocBuilder&lt;...&gt;(
        builder: (context, state) {
          ...
          if (state is NotAuthenticated) {
            return AppOnboarding(key: _appOnboardingKey);
          }
          ...
        }
      ),
    }
  }
  </code></pre>
  <p>I was a little surprised this didn't work. Do global keys not maintain the state of the entrie widget-subtree?</p>