简体   繁体   中英

Flutter: Dropdownbutton does not update to display selected choice

I am trying to use a dropdown button to have the user select from a list of options, however after making a selection the dropdown button remains displaying the hint. I think something about the setState is not updating the dropdown button.

              value: skillChoice,
              items: listDrop,
              hint: Text("Choose Skill"),
              onChanged: (value) {

                setState(() {
                  skillChoice = value;
                });
              },
            ),

here are the variables which are declared earlier in the code:

      List<DropdownMenuItem<int>> listDrop = [];
      int skillChoice = null;

Can anyone let me know why it isn't updating?

I think setting skillChoice null initially disables the dropdown.

It would have been better if you had shown the full code snippet of how you implemented your DropDownButton. But here is how I do implement mine:

 // This is the initial value that will be selected on the DropDownMenu by default

    String choice = '1';


    // This is the List of String (or whatever data type you want) that will make up the 
    Drop Down Menu Item options. NOTE: That the String value of 'choice' ('1') is also present in the List of choices ('1')

    List<String> choices = [
    '1',
    '2',
    '3',
    '4',
    '5',
    ];

    DropdownButton<String>(
                value: choice,
                icon: Icon(Icons.add),
                onChanged: (String newValue) {
                  setState(() => choices = newValue);
                },
                items: choices.map<DropdownMenuItem<String>>((String value) {
                  return DropdownMenuItem<String>(
                    value: value,
                    child: Text(
                          value,
                        )
                  );
                }).toList(),
              ),

Should work properly for you now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM