I am trying to take JSON returned from rest api and populate a dropdownmenu, I need to have the value different from the child value. Has anyone done this or have an exampl
You are getting this error because you are initializing _referPractice
with a value that !null
, and you are feeding it to the property value
of the DropDownButton
, which represents the currently selected item, and has to be null if no item has been selected yet.
I have replicated your example using the JSON you provided:
String _mySelection;
List<Map> _myJson = [{"id":0,"name":"<New>"},{"id":1,"name":"Test Practice"}];
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new DropdownButton<String>(
isDense: true,
hint: new Text("Select"),
value: _mySelection,
onChanged: (String newValue) {
setState(() {
_mySelection = newValue;
});
print (_mySelection);
},
items: _myJson.map((Map map) {
return new DropdownMenuItem<String>(
value: map["id"].toString(),
child: new Text(
map["name"],
),
);
}).toList(),
),
),
);
}