简体   繁体   English

通过选择 flutter 中的值未更新下拉按钮

[英]DropdownButton not updated by selecting a value in flutter

Whenever I try to update the value into the desired one it doesn't change每当我尝试将值更新为所需的值时,它都不会改变

Here's a snippet of the relevant code:以下是相关代码的片段:

new DropdownButton<String>(
            items: <String>['A', 'B', 'C', 'D'].map((String value) {
              return new DropdownMenuItem<String>(
                value: value,
                child: new Text(value),
              );
            }).toList(),
              onChanged: (value) {
                setState(() {
                  value1 = value!;
                });
              }
          )

Looks like the issue is that the name of the variable you're updating in setState is named value1, while the variable you assign to the value parameter in your DropdownMenuItem is named value.看起来问题是您在 setState 中更新的变量的名称被命名为 value1,而您在 DropdownMenuItem 中分配给 value 参数的变量被命名为 value。

Try using the following code for onChanged:尝试对 onChanged 使用以下代码:

onChanged: (newValue) {
            setState(() {
              value = newValue;
            });
          }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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