[英]Can't create DropdownButton with List of objects in Flutter
这是我得到的错误:
这是我的 DropdownButton 代码:
DropdownButton countryPicker() {
return DropdownButton<String>(
onChanged: (value) {
setState(() {
country = value;
});
},
value: country,
hint: Text(country),
items: countries(),
);
}
List<DropdownMenuItem<String>> countries() {
List<DropdownMenuItem<String>> list = [];
countryDetails.forEach((c) {
list.add(DropdownMenuItem<String>(
child: Text(c.countryName),
value: c.countryName,
));
});
return list;
}
我所有的数据列表都在 countryDetails 变量中。
清单是这样的:
[
{
"country_name": "Andorra",
"alpha2_code": "AD",
"states": [
{"state_name": "Andorra la Vella"},
{"state_name": "Canillo"},
]
},
..............
..............
]
那么,这里有什么问题呢?
在错误中,它说您有两个或多个DropdownMenuItem
具有相同的value
,这是不允许的。
当我查看您的代码时,您似乎正在传递c.countryName
作为值,但是您能否仔细检查并确保您的所有country_name
都是唯一的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.