flutter - 元素类型“String”不能分配给列表类型“DropdownMenuItem”'
问题描述
我想用一些字符串填充 Flutter 中的 DropdownButton,但出现错误
The element type 'String' can't be assigned to the list type 'DropdownMenuItem<String>'
关于用字符串归档列表
这是我的代码片段:
DropdownButton<String>(
value: filter.getFilterPersonality(),
onChanged: (String newValue){filter.setFilterPersonality(newValue);},
items: ["-"],
),
我究竟做错了什么?
解决方案
items
应该是 a List
of DropdownMenuItem<String>
not a List<String>
,只有“-”。
DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.deepPurple),
underline: Container(
height: 2,
color: Colors.deepPurpleAccent,
),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['One', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
);
见这里: https ://api.flutter.dev/flutter/material/DropdownMenuItem/DropdownMenuItem.html
推荐阅读
- python - PyPDF2:从 zip 文件中读取 pdf
- c - 尝试在两个文件之间交换数据时出现问题
- javascript - 将值 date.now() "JS" 转换为日期格式 "MM/dd/yyyy HH:mm:ss"
- sql - 将 0019 年更改为 2019
- python - 文件未正确到达
- python - 另一个线程在等待 HTTP 响应时会执行另一个线程吗?
- jquery - jquery重置数组中的所有输入字段
- c++ - 选择排序时交换函数调用的次数和完成的交换次数是否相同?
- android - 地图类型 GoogleMap.MAP_TYPE_NONE 的 Google 地图背景颜色
- c++ - cudaMalloc() 是否将数组初始化为 0?