title: Flutter save List with shared preferences
body: <p>I try to save a list of strings permanent to the device. When trying to add a List through a pop-up dialog I the following error is displayed</p>
  
  <p><em>E/flutter (10074): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method 'add' was called on null.</em></p>
  
  <p><em>E/flutter (10074): Receiver: null</em></p>
  
  <p><em>E/flutter (10074): Tried calling: add("drinks")</em></p>
  
  <p>Further on the ListView is not displayed when opening the page. (It opens after you enter the on screen dialog)</p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:shared_preferences/shared_preferences.dart';
  
  class Categories extends StatefulWidget {
    @override
    _CategoriesState createState() =&gt; _CategoriesState();
  }
  
  class _CategoriesState extends State&lt;Categories&gt; {
    List&lt;String&gt; categoryList = List&lt;String&gt;();
    TextEditingController _textFieldController = TextEditingController();
    int count = 0;
  
    @override
    Widget build(BuildContext context) {
      if (categoryList == null) {
        setState(() {
          categoryList = ['Food', 'Clothes'];
        });
      }
      _update();
      return Scaffold(
        appBar: AppBar(
          title: Text("Categories"),
        ),
        body: SafeArea(
          child: Container(
            color: Colors.white,
            child: getCategoriesListView(),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          onPressed: () {
            setState(() {
              _displayDialog(context);
            });
          },
        ),
      );
    }
  
    ListView getCategoriesListView() {
      return ListView.builder(
          itemCount: categoryList.length,
          itemBuilder: (context, int position) {
            return Card(
              color: Colors.white,
              elevation: 2.0,
              child: ListTile(
                title: Text(categoryList[position]),
                trailing: GestureDetector(
                  child: Icon(
                    Icons.delete,
                    color: Colors.grey,
                  ),
                  onTap: () {
                    setState(() {
                      _delete(context, categoryList[position]);
                    });
                  },
                ),
              ),
            );
          });
    }
  
    void _add(BuildContext context, String category) async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      categoryList.add(category);
      prefs.setStringList('Categories', categoryList);
      showSnackBar(context, 'Your category was added');
    }
  
    void _delete(BuildContext context, String category) async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      categoryList.remove(category);
      prefs.setStringList('Categories', categoryList);
      showSnackBar(context, 'Your category was removed');
    }
  
    void _update() async {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      categoryList = prefs.getStringList('Categories');
    }
  
    void showSnackBar(BuildContext context, String message) async {
      final snackBar = SnackBar(content: Text(message));
      Scaffold.of(context).showSnackBar((snackBar));
    }
  
    _displayDialog(BuildContext context) async {
      return showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              title: Text('Add new category'),
              content: TextField(
                controller: _textFieldController,
              ),
              actions: &lt;Widget&gt;[
                FlatButton(
                  child: Text('ADD'),
                  onPressed: () {
                    setState(() {
                      String name = _textFieldController.text;
                      print(name);
                      _add(context, name);
                      Navigator.of(context).pop();
                    });
                  },
                ),
                FlatButton(
                  child: Text('CANCEL'),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
              ],
            );
          });
    }
  }
  </code></pre>