title: How to clear all items from an AnimatedList in Flutter
body: <p>I was trying to preproduce all of the update operations on an AnimatedList like I have done for a RecyclerView adapter in Android. But when I got to clearing all of the data, I couldn't figure out how to do it.</p>
  
  <p>If I do this </p>
  
  <pre><code>setState(() {
    _data.clear();
  });
  </code></pre>
  
  <p>then the backing data is cleared but the GlobalKey's current state still doesn't know about the change, and there is no <code>clear()</code> method on it.</p>
  
  <p>Supplemental code</p>
  
  <pre><code>import 'package:flutter/material.dart';
  
  void main() =&gt; runApp(MyApp());
  
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        debugShowCheckedModeBanner: false,
        home: Scaffold(
          appBar: AppBar(title: Text('Update AnimatedList data')),
          body: BodyWidget(),
        ),
      );
    }
  }
  
  class BodyWidget extends StatefulWidget {
    @override
    BodyWidgetState createState() {
      return new BodyWidgetState();
    }
  }
  
  class BodyWidgetState extends State&lt;BodyWidget&gt; {
  
    // the GlobalKey is needed to animate the list
    final GlobalKey&lt;AnimatedListState&gt; _listKey = GlobalKey();
  
    // backing data
    List&lt;String&gt; _data = ['Horse', 'Cow', 'Camel', 'Sheep', 'Goat'];
  
    @override
    Widget build(BuildContext context) {
      return Column(
        children: &lt;Widget&gt;[
          SizedBox(
            height: 400,
            child: AnimatedList(
              key: _listKey,
              initialItemCount: _data.length,
              itemBuilder: (context, index, animation) {
                return _buildItem(_data[index], animation);
              },
            ),
          ),
          RaisedButton(
            child: Text(
              'Clear all items',
              style: TextStyle(fontSize: 20),
            ),
            onPressed: () {
              _clearAllItems();
            },
          )
        ],
      );
    }
  
    Widget _buildItem(String item, Animation animation) {
      return SizeTransition(
        sizeFactor: animation,
        child: Card(
          child: ListTile(
            title: Text(
              item,
              style: TextStyle(fontSize: 20),
            ),
          ),
        ),
      );
    }
  
    void _clearAllItems() {
      _data.clear();
      // no such method
      _listKey.currentState.clear();
    }
  
  }
  </code></pre>