Dropdown selection not displaying with Flutter

后端 未结 8 2113
攒了一身酷
攒了一身酷 2021-01-12 08:56

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

8条回答
  •  遥遥无期
    2021-01-12 09:39

    For those who are facing this problem in future. Use variable to set the value to DropdownButton and when the onChanged called update the variable using setState

    Expanded(child: DropdownButton(
              items: cities.map((String value) {
                return DropdownMenuItem(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
              value: city,
              onChanged: (String val) {
                _onDropDownChanged(val);
              },
            ),)
    

    update the city variable by using state

    void _onDropDownChanged(String val) {
        setState(() {
          this.city = val;
        });
      }
    

    For further information check the link below

    https://github.com/FazalHussain/Flutter-Demo/blob/trip_cost_app/lib/main.dart

提交回复
热议问题