My fetchCities()
method returns Future<List<City>>
and it loads data from rest webservice. Code to populate items:
Widget buildCitiesSelector(){
return new Center(
child: FutureBuilder(
future: fetchCities() ,
builder: (context, snapshot){
if (snapshot.hasData) {
return new DropdownButton <City>(
hint: Text('Wybierz miasto'),
items: snapshot.data.map<DropdownMenuItem<City>>((City value) {
return DropdownMenuItem<City>(
value: value,
child: Text(value.name),
);
}).toList(),
onChanged: (value) {
setState(() {_selectedCity = value;});
},
value: _selectedCity,
);
}
else{
return CircularProgressIndicator();
}
}
)
);
}
Result: items are correctly displayed in selector. However, when selecting any particular item, I'm getting exception:
I/flutter (13910): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (13910): The following assertion was thrown building FutureBuilder>(dirty, state: I/flutter (13910): _FutureBuilderState>#dacd9): I/flutter (13910): 'package:flutter/src/material/dropdown.dart': Failed assertion: line 560 pos 15: 'items == null || I/flutter (13910): items.isEmpty || value == null || items.where((DropdownMenuItem item) => item.value == I/flutter (13910): value).length == 1': is not true.
How to properly select item? Any ideas what's wrong?