我这里有一张地图清单:
var taxesGroups = [
{
"name": "Spain",
"taxes": [
{"name": "IVA", "percentage": "21"},
{"name": "IRPF", "percentage": "19"},
]
},
{
"name": "UK",
"taxes": [
{"name": "VAT", "percentage": "20"},
]
}
];
var dropdownValue = taxesGroups[0]["name"];
到目前为止,我已经尝试过了:
DropdownButton(
value: dropdownValue,
items: taxesGroups.map((taxGroup) {
return DropdownMenuItem<String>(
value: taxGroup["name"],
child: Text(taxGroup["name"]),
);
}).toList(),
onChanged: (taxGroup) {
setState(() {
dropdownValue = taxGroup;
});
},
),
我收到此错误:
类型“列表”不是类型“列表<DropdownMenuItem>”的子类型
我想这与我要在下拉菜单(英国,英国)中显示的信息以及选择一个选项后得到的信息有关,但我不知道
您不匹配taxGroups地图。
我试图以此重新创建您的用例,并且它可以工作。
class MyApp2 extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp2> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
var taxesGroups = [
{
"name": "Spain",
"taxes": [
{"name": "IVA", "percentage": "21"},
{"name": "IRPF", "percentage": "19"},
]
},
{
"name": "UK",
"taxes": [
{"name": "VAT", "percentage": "20"},
]
}
];
var dropdownValue;
@override
void initState() {
dropdownValue = taxesGroups[0];
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("AppBar"),
),
body: DropdownButton(
value: dropdownValue['name'],
items: taxesGroups.map((taxGroup) {
return DropdownMenuItem<String>(
value: taxGroup["name"],
child: Text(taxGroup["name"]),
);
}).toList(),
onChanged: (taxGroup) {
print('taxGroup $taxGroup');
taxesGroups.map((e) {
if (e["name"] == taxGroup)
setState(() {
dropdownValue = e;
});
});
},
),
);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句