gpt4 book ai didi

flutter - 预期值为 'List>'类型,但获得了 'MappedListIterable>'类型之一

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

我正在尝试从下拉菜单中更改主题颜色,但出现此错误:“预期类型为'List>'的值,但类型为之一
'MappedListIterable>'

Color  selected ;

MaterialApp(
theme: ThemeData(primarySwatch: selected),
home: Scaffold();

List myColor = [
Colors.amber,
Colors.blue,
Colors.green,
Colors.black,
];

DropdownButton(
icon: Icon(Icons.arrow_downward),
value: selected,
onChanged: (value){
setState(() {
selected = value;
});
},
items: myColor.map((value)=>DropdownMenuItem(
value: value,
child: Text(value.toString()),
))
),

最佳答案

DropdownButton需要显式类型才能与其项目正确匹配。您也不会将map返回的可迭代对象返回到列表中:

// Before, it's type was List<dynamic>
List<Color> myColor = [ // Add <Color>
Colors.amber,
Colors.blue,
Colors.green,
Colors.black,
];



items: myColor.map(
(value) => DropdownMenuItem(
value: value,
child: Text(value.toString()),
),
).toList(), // Add toList()

有关更多详细信息,请参见 DropdownButton docs

关于flutter - 预期值为 'List<DropdownMenuItem<Color>>'类型,但获得了 'MappedListIterable<Color, DropdownMenuItem<Color>>'类型之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59791964/

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