title: Flutter: UniqueKey not working when removing/recreating widget
body: <p><strong>Situation:</strong>
  In my app I have input cards (with like sliders) which are displayed/hidden by clicking a button.</p>
  <p><strong>Problem:</strong>
  When I edit the value of a slider, then hide and unhide again, the value is lost.</p>
  <p>I tried using a UniqueKey() but it doesn't solve the problem.</p>
  <p>A workaround which doesn't work for me is storing the value elsewhere and the passing it as an initial value to the slider. This is because there are more variables in the state than just the slider value I have to preserve.</p>
  <p><a href="https://dartpad.dev/b6409e10de32b280b8938aa75364fa7b?" rel="nofollow noreferrer">Run on Dartpad</a></p>
  <pre class="lang-dart prettyprint-override"><code>import 'package:flutter/material.dart';
  
  final _someKey = UniqueKey();
  
  void main() =&gt; runApp(MyApp());
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) =&gt; MaterialApp(home: MyHomePage());
  }
  class MyHomePage extends StatefulWidget {
    MyHomePage();
    @override
    _MyHomePageState createState() =&gt; _MyHomePageState();
  }
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
    @override
    Widget build(BuildContext context) =&gt; Scaffold(
        appBar: AppBar(title: Text(&quot;Problem: UniqueKey not working&quot;)),
        body: ContentHider(
          child: Column(children: [
            // This Slider DOES change when hiding/unhiding
            // This Slider SHOULDN'T change
            CustomWidget(
              key: _someKey,
            ),
          ]),
        ));
  }
  
  // When pressing the button the &quot;child&quot; gets rendered / not rendered
  class ContentHider extends StatefulWidget {
    final Widget child;
    ContentHider({this.child});
    @override
    _ContentHiderState createState() =&gt; _ContentHiderState();
  }
  
  class _ContentHiderState extends State&lt;ContentHider&gt; {
    bool hidden = false;
    @override
    Widget build(BuildContext context) =&gt; Center(
          child: Column(
            children: [
              RaisedButton(
                onPressed: () {
                  if (hidden == false)
                    setState(() =&gt; hidden = true);
                  else
                    setState(() =&gt; hidden = false);
                },
                child: Text(&quot;Hide/Unhide&quot;),
              ),
              hidden ? Container() : widget.child
            ],
          ),
        );
  }
  
  // StatfulWidget which contains changable content -&gt; Slider
  class CustomWidget extends StatefulWidget {
    CustomWidget({Key key}) : super(key: key);
    @override
    _CustomWidgetState createState() =&gt; _CustomWidgetState();
  }
  
  class _CustomWidgetState extends State&lt;CustomWidget&gt; {
    double _currentSliderValue = 0.0;
    @override
    Widget build(BuildContext context) {
      return Card(
        color: Color(0xFFACFFBC),
        child: Slider(
        value: _currentSliderValue,
        min: 0,
        max: 100,
        label: _currentSliderValue.round().toString(),
        onChanged: (double value) {
          setState(() {
            _currentSliderValue = value;
          });
        },
        ),
      );
    }
  }
  </code></pre>