Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 在颤振中下拉按钮选择未更新_Flutter_Button_Dropdown - Fatal编程技术网

Flutter 在颤振中下拉按钮选择未更新

Flutter 在颤振中下拉按钮选择未更新,flutter,button,dropdown,Flutter,Button,Dropdown,我创建了我的下拉按钮,但无论何时选择一个项目都不会更新值。但无论何时重新加载所有值​​更新。有人能猜出原因吗?还是有解决办法 DropdownButton<String>( value: dropdownValue, icon: Icon(Icons.keyboard_arrow_down), iconSize: 15, el

我创建了我的下拉按钮,但无论何时选择一个项目都不会更新值。但无论何时重新加载所有值​​更新。有人能猜出原因吗?还是有解决办法

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(),
                  ),
下拉按钮(
value:dropdownValue,
图标:图标(图标。键盘箭头向下),
iconSize:15,
标高:10,
样式:TextStyle(颜色:Colors.grey),
一旦更改:(newValue){
setState(()异步{
dropdownValue=newValue;
final selectedLanguage=wait SharedPreferences.getInstance();
selectedLanguage.setString('selectedLanguage',dropdownValue);
});
allTranslations.setNewLanguage(
allTranslations.getLocaleKey(
下拉值,
),
);
},
项目:[
英语课文,
中文文本,
俄罗斯文本,
].map((字符串值){
返回下拉菜单项(
价值:价值,
子项:文本(值),
);
}).toList(),
),

@kimSoo很好,它起作用了

每个人的解决方案是
setstate
中的异步闭包不起作用。我们必须删除它并提前初始化共享首选项

初始化
final selectedLanguage=wait SharedPreferences.getInstance()更早

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(),
                  ),
下拉按钮(
value:dropdownValue,
图标:图标(图标。键盘箭头向下),
iconSize:15,
标高:10,
样式:TextStyle(颜色:Colors.grey),
一旦更改:(newValue){

setState((){我可以在不同的上下文中记住相同的行为。我认为这是因为您在setState中使用了异步闭包。请尝试删除async并初始化您的Wait SharedReferences.getInstance()在这个功能之前,它是有效的!谢谢“MarcelDznice听到!我把它当作答案,所以其他人不会错过解决方案。如果你把它标记为被接受的答案,我会很感激的。问候!