简体   繁体   English

无法使用 Flutter 中的对象列表创建 DropdownButton

[英]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.

相关问题 在 Flutter 中为 DropdownButton 创建项目列表 - Create Item list for DropdownButton in Flutter 我想在 flutter 中创建一个可重复使用的自定义 DropdownButton,这里提示名称和列表对于所有 DropdownButton 都是不同的 - I want to create a custom DropdownButton in flutter which can be reusable, here the hint name and list will be different for all DropdownButton 我想在 flutter 中创建一个自定义 DropdownButton,它可以与不同的列表一起重复使用 [Flutter] - I want to create a custom DropdownButton in flutter which can be reusable with different List [Flutter] 如何使用颤振中的列表中的 JSON 数据列表创建 DropdownButton - How to Create DropdownButton with a list of JSON data within a list in flutter 数据表中的 Flutter DropdownButton,列表中的 DropdownButton 选项 - Flutter DropdownButton in DataTable, DropdownButton options from list 如何使用 JSON 数据列表创建 DropdownButton,我希望它在 Flutter 中填充我的 DropDownButton - How to Create DropdownButton with a list of JSON Data and I want it to populate my DropDownButton in Flutter Flutter:DropDownButton 列表从一开始就没有显示 - Flutter: DropDownButton list is not displayed from its start flutter动态列表如何使用DropDownButton? - How to use DropDownButton for dynamic list in flutter? 下拉按钮 Flutter - Dropdownbutton Flutter 颤动下拉按钮 - Flutter DropDownButton
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM