title: Add data to local storage in flutter
body: <p>I use <a href="https://pub.dev/packages/localstorage" rel="nofollow noreferrer">https://pub.dev/packages/localstorage</a> to create local storage in my Flutter app.<br>
  I created some method to use with this local storage in this 'Event' widget</p>
  
  <pre><code>class Event extends StatefulWidget {
    final eventInfo;
    Event({Key key, List eventInfo})
        : this.eventInfo = eventInfo,
          super(key: key);
  
    @override
    _EventState createState() =&gt; _EventState();
  }
  
  class FavName {
    String eventName;
    FavName({this.eventName});
  
    get favEventName {
      return eventName;
    }
  
    toJSONEncodable() {
      Map&lt;String, dynamic&gt; m = new Map();
  
      m['Name'] = eventName;
  
      return m;
    }
  }
  
  class FavList {
    List&lt;FavName&gt; favNameList;
  
    FavList() {
      favNameList = new List();
    }
  
    toJSONEncodable() {
      return favNameList.map((item) {
        return item.toJSONEncodable();
      }).toList();
    }
  }
  
  class _EventState extends State&lt;Event&gt; {
  
    final FavList list = new FavList();
    final LocalStorage storage = new LocalStorage('favList');
  
    addItem(String eventName) {
      setState(() {
        final item = new FavName(eventName: eventName);
        list.favNameList.add(item);
        _saveToStorage();
      });
    }
  
    deleteItem(String eventName) {
      setState(() {
        final item = new FavName(eventName: eventName);
        list.favNameList.remove(item);
        _saveToStorage();
      });
    }
  
    _saveToStorage() {
      storage.setItem('favList', list.toJSONEncodable());
    }
  
   . . .
  </code></pre>
  
  <p>So I wanted to add a String into my local storage with a button.<br>
  Here is my code for the button.</p>
  
  <pre><code>child: IconButton(
                  icon: Icon(
                    Icons.star,
                    color: _iconColors,
                  ),
                  onPressed: () {
                    print(name);
                    (storage.getItem(name)) ? deleteItem(name)
                    : addItem(name);
                    setState(() {
                      if (storage.getItem(name) == null) {
                        _iconColors = Colors.grey;
                      } else {
                        _iconColors = Colors.yellow;
                      }
                    });
                  })
  </code></pre>
  
  <p>But when I tap on the button, I get this error</p>
  
  <pre><code>I/flutter (27489): 2-Art Event          //Print the string that I want to add to the local storage
  
  ════════ Exception caught by gesture ═══════════════════════════════════════════
  The following assertion was thrown while handling a gesture:
  Failed assertion: boolean expression must not be null
  
  When the exception was thrown, this was the stack
  #0      _EventState.buildInfo.&lt;anonymous closure&gt; 
  package:lx_building/screens/event.dart:190
  #1      _InkResponseState._handleTap 
  package:flutter/…/material/ink_well.dart:772
  #2      _InkResponseState.build.&lt;anonymous closure&gt; 
  package:flutter/…/material/ink_well.dart:855
  #3      GestureRecognizer.invokeCallback 
  package:flutter/…/gestures/recognizer.dart:182
  #4      TapGestureRecognizer.handleTapUp 
  package:flutter/…/gestures/tap.dart:522
  ...
  Handler: "onTap"
  Recognizer: TapGestureRecognizer#1f697
      debugOwner: GestureDetector
      state: possible
      won arena
      finalPosition: Offset(324.7, 264.3)
      finalLocalPosition: Offset(32.4, 9.7)
      button: 1
      sent tap down
  ════════════════════════════════════════════════════════════════════════════════
  </code></pre>
  
  <p>How can I fix this?</p>