簡體   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