无法将元素类型'String'分配给列表类型"下拉列表菜单项<String>"



我想用一些字符串填充 Flutter 中的下拉按钮,但我收到错误

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应该是DropdownMenuItem<String>List,而不是只有"-"的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

DropdownButton 中的项应位于列表中。请注意如何将地图转换为以下示例代码中最后一个的列表。

_currencies是一个字符串数组。

items: _currencies.map((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(
value,
style: textStyle,
),
);
}).toList(),

相关内容

最新更新