Home > Net >  Why setstate function not working with dropdown
Why setstate function not working with dropdown

Time:06-15

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,
  • Related