title: Updating Dropdown Data In Flutter Gives Error
body: <p>I am in the process of developing a widget that consists of a <code>dropdownbutton</code> in Flutter. This widget creates <code>dropdownmenuitems</code> after making a request to a url the user has passed in.</p>
  
  <p>I have seen the call being made and the response being valid, but when I use <code>setState</code> to update the data source for the dropdown I get the following error:</p>
  
  <blockquote>
    <p>'package:flutter/src/material/dropdown.dart': Failed assertion: line 560 pos 15: 'items == null ||
    I/flutter (11514): items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value ==
    I/flutter (11514): value).length == 1': is not true.</p>
  </blockquote>
  
  <p>I tried researching this error and found no helpful answers. The code is below:</p>
  
  <pre><code>class _MyWidgetState extends State&lt;MyWidget&gt; {
  
  List&lt;DropdownMenuItem&lt;String&gt;&gt; _data = [];
  String _selected = '';
  
  @override
  void initState() {
     super.initState();
     _loadData();
   }
   void _loadData() async {
      if (widget.urlToFetchData.isNotEmpty) {
        var response = await http.get(widget.urlToFetchData);
        if (response.statusCode == 200) {
           Map&lt;String, dynamic&gt; jsonResponse = convert.jsonDecode(response.body);
           jsonResponse.forEach((key, value){
             setState(() {
                    this._data.add(new DropdownMenuItem(
                                    child: new Text(value.toString()),
                                    value: value.toString(),
                               ));
             });
           });
        } else {
           print("Request failed with status: ${response.statusCode}.");
         }
      }
    }
  }
  
  @override
  Widget build(BuildContext context) {
     if (_data.length == 0) {
        return new Container();
     } else {
          return Column(
              children: &lt;Widget&gt; [
                    new Text(
                      widget.dropdownLabelTitle
                   ),
                  DropdownButton(
                       value: _selected,
                       items: _data,
                       hint: new Text(widget.defaultOptionText),
                       onChanged: (value) {
                          _selected = value;
                          widget.valueReturned(_selected);
                          setState(() {
  
                          });
                       }
                     )
                  ],
                );
              }
         }
      }
  </code></pre>
  
  <p>Now, I am fully aware the at the initialization of the widget, the items field in the dropdownbutton is initialized with an empty list, but I thought that by calling setState when the http call finishes, that will update the value.</p>
  
  <p>I have tried different methods of updating the data(by creating a local list and then using <code>addAll</code> or just by assignment), but I get the same error.</p>
  
  <p>Anyone know how to solve this issue?</p>