Dropdown selection not displaying with Flutter

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

    make sure you are not declaring the selectedValue inside the Widget the below example works for me perfectly.

    here is the working code on dartpad to test it out

    var currentSelectedValue;
    const deviceTypes = ["Mac", "Windows", "Mobile"];
    
     Widget typeFieldWidget() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20),
      child: FormField(
        builder: (FormFieldState state) {
          return InputDecorator(
            decoration: InputDecoration(
                border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(5.0))),
            child: DropdownButtonHideUnderline(
              child: DropdownButton(
                hint: Text("Select Device"),
                value: currentSelectedValue,
                isDense: true,
                onChanged: (newValue) {
                  setState(() {
                    currentSelectedValue = newValue;
                  });
                  print(currentSelectedValue);
                },
                items: deviceTypes.map((String value) {
                  return DropdownMenuItem(
                    value: value,
                    child: Text(value),
                  );
                }).toList(),
              ),
            ),
          );
        },
      ),
    );
    }
    

提交回复
热议问题