I made a dropdown in flutter that takes a Map<String, int> as a list of values.But setState doesnt work.What am i doing wrong again
Map<String, int> grades = {
"AA": 5,
"BB": 4,
};
int val=5;
@override
Widget build(BuildContext context) {
return Scaffold(
body: DropdownButton<int>(
value: grades["AA"],
onChanged: (newVal){
setState((){
if(newVal!=null){ val=newVal;}
});
},
items: grades
.map((key, value) {
return MapEntry(
key,
DropdownMenuItem<int>(
value: value,
child: Text(key),
));
})
.values
.toList(),
));
CodePudding user response:
DropdownButton
value is hard-coded(value: grades["AA"],
), replace
body: DropdownButton<int>(
value: grades["AA"],
With
body: DropdownButton<int>(
value: val,