Dropdown selection not displaying with Flutter

后端 未结 8 2098
攒了一身酷
攒了一身酷 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:38

    You hard-coded the first city as value for the dropdown.

    new DropdownButton(
                value: cities.elementAt(0), //this one
    ...
    

    You need to have some state variable to hold the selected city.

    class YourStatefulWidgetState extends State {
      @override
      initState() {
       super.initState();
       selectedCity = cities.elementAt(0)
      }
    
     @override
     Widget build(BuildContext context) {
       ...
       new DropdownButton(
                value: selectedCity,
                hint: new Text("Ville"),
                items: cities.map((String value) {
                  return new DropdownMenuItem(value: value,
                    child: new Row(
                      children: [
                        new Icon(
                          Icons.location_city, color: Colors.deepOrange,),
                        new Text(value)
                      ],
                    ),);
                }).toList(),
                onChanged: (String value) {
                  setState(() {
                    selectedCity = getTravelCity(value);
                  });
                },),
         ...
    

提交回复
热议问题