flutter - 尝试显示字符串时,DropdownButtonFormField给出错误

标签 flutter dart drop-down-menu widget

我想制作显示如下城市的DropdownButtonFormField:

final List<String> cities = ['London', 'New York', 'Paris', 'Shanghahai', 'Tokyo'];

String _currentCity;

DropdownButtonFormField(
            value: _currentCity ?? ' ',
            decoration: textInputDecoration,
            items: cities.map((city) {
              return DropdownMenuItem(
                value: city,
                child: Text('$city City '),
              );
            }).toList(),
            onChanged: (val) => setState(() => _currentCity = val ),
          ),

但是当我尝试显示这些字符串时,出现红色屏幕错误:
enter image description here

如果我将列表中的字符串更改为数字,则效果很好:
 final List<String> cities = ['0', '1', '2', '3', '4'];

有人可以告诉我它有什么问题吗,尽管我使用字符串列表

最佳答案

您可以尝试将_currentCity设置为默认值吗?

String _currentCity = 'London';

关于flutter - 尝试显示字符串时,DropdownButtonFormField给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62135600/

相关文章:

flutter - 如何用列中的空值解决问题?

flutter - 在 Flutter 中,子部件如何防止其可滚动父部件的滚动?

list - Flutter List行为不符合预期

generics - 如何在 Dart 中使用镜像获取泛型类型变量的具体类型?

charts - _ScaffoldLayout 自定义多子布局委托(delegate)忘记布局以下子级

flutter - native 文件路径

dart - 类重置变量

HTML 和 CSS 下拉菜单

css - 当鼠标悬停在第二个元素上时,简单的 CSS 下拉菜单消失

flutter - 如何在 flutter 中将元素推到最右边