繁体   English   中英

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM