Search code examples
flutterbuttondropdown

Dropdown button selection not update in Flutter


I created my dropdown button, but whenever an item is selected does not update value.But whenever I hotreload all my values ​​are updated. Can anyone guess the reason? or is there a solution?

DropdownButton<String>(
                    value: dropdownValue,
                    icon: Icon(Icons.keyboard_arrow_down),
                    iconSize: 15,
                    elevation: 10,
                    style: TextStyle(color: Colors.grey),
                    onChanged: (newValue) {
                      setState(() async {
                        dropdownValue = newValue;
                    final selectedLanguage= await SharedPreferences.getInstance();
                    selectedLanguage.setString('selectedLanguage', dropdownValue);
                      });
                      allTranslations.setNewLanguage(
                        allTranslations.getLocaleKey(
                          dropdownValue,
                        ),
                      );
                    },
                    items: <String>[
                      englishText,
                      chineseText,
                      russianText,
                    ].map<DropdownMenuItem<String>>((String value) {
                      return DropdownMenuItem<String>(
                        value: value,
                        child: Text(value),
                      );
                    }).toList(),
                  ),

Solution

  • @kimSoo great that it worked!

    Solution for everyone is the async closure in setstate wont work. We have to remove it and initialize Shared Preferences earlier.

    Initialize final selectedLanguage= await SharedPreferences.getInstance(); earlier

    DropdownButton<String>(
                        value: dropdownValue,
                        icon: Icon(Icons.keyboard_arrow_down),
                        iconSize: 15,
                        elevation: 10,
                        style: TextStyle(color: Colors.grey),
                        onChanged: (newValue) {
                          setState(() { <----- removed async
                            dropdownValue = newValue;
                        
                        selectedLanguage.setString('selectedLanguage', dropdownValue);
                          });
                          allTranslations.setNewLanguage(
                            allTranslations.getLocaleKey(
                              dropdownValue,
                            ),
                          );
                        },
                        items: <String>[
                          englishText,
                          chineseText,
                          russianText,
                        ].map<DropdownMenuItem<String>>((String value) {
                          return DropdownMenuItem<String>(
                            value: value,
                            child: Text(value),
                          );
                        }).toList(),
                      ),