[英]Can't create DropdownButton with List of objects in Flutter
This is the error I have got:这是我得到的错误:
Here is my DropdownButton code:这是我的 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;
}
All of my data list is in countryDetails variable.我所有的数据列表都在 countryDetails 变量中。
And the list is like this:清单是这样的:
[
{
"country_name": "Andorra",
"alpha2_code": "AD",
"states": [
{"state_name": "Andorra la Vella"},
{"state_name": "Canillo"},
]
},
..............
..............
]
So, whats the problem here?那么,这里有什么问题呢?
In the error it says you have two or more DropdownMenuItem
with the same value
, which is not allowed.在错误中,它说您有两个或多个
DropdownMenuItem
具有相同的value
,这是不允许的。
When I take a look at your code, it seems like you are passing c.countryName
as the value, but could you double check and make sure that all of your country_name
are unique?当我查看您的代码时,您似乎正在传递
c.countryName
作为值,但是您能否仔细检查并确保您的所有country_name
都是唯一的?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.