gpt4 book ai didi

flutter - 元素类型 'String'无法分配给列表类型 'DropdownMenuItem'

转载 作者:行者123 更新时间:2023-12-03 04:22:18 25 4
gpt4 key购买 nike

我想用一些字符串填充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应该是ListDropdownMenuItem<String>,而不是仅带有“-”的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

关于flutter - 元素类型 'String'无法分配给列表类型 'DropdownMenuItem<String>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61963657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com