我想制作显示如下城市的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 ),
),
但是当我尝试显示这些字符串时,出现红色屏幕错误:
如果我将列表中的字符串更改为数字,则效果很好:
final List<String> cities = ['0', '1', '2', '3', '4'];
有人可以告诉我它有什么问题吗,尽管我使用字符串列表
最佳答案
您可以尝试将_currentCity设置为默认值吗?
String _currentCity = 'London';
关于flutter - 尝试显示字符串时,DropdownButtonFormField给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62135600/