I\' m gettings items from JSON and displaying them in a dropdown. When the person select an item from the dropdown list, I get the selection but the selected item doesn\'t c
this was a bug reported in the Github of Flutter.
The correct implementions is:
class _DropdownButtonBugState extends State<DropdownButtonBug> {
final List<String> _items = ['One', 'Two', 'Three', 'Four'].toList();
String _selection;
@override
void initState() {
_selection = _items.first;
super.initState();
}
@override
Widget build(BuildContext context) {
final dropdownMenuOptions = _items
.map((String item) =>
new DropdownMenuItem<String>(value: item, child: new Text(item))
)
.toList();
return new Scaffold(
body: new DropdownButton<String>(
value: _selection,
items: dropdownMenuOptions,
onChanged: (s) {
setState(() {
_selection = s;
});
}
)
);
}
}
The Dropdown Button can be wrapped inside a StatefulBuilder() widget if the selected values are not updated after the selction.