Api 从下拉选择中获取完整信息(颤振/飞镖)

Api 从下拉选择中获取完整信息(颤振/飞镖),api,flutter,dart,dropdown,Api,Flutter,Dart,Dropdown,我正在制作一个corona应用程序,如果你从下拉列表中选择一个国家,它会显示感染者、死亡者和康复者的数量。 当我将值项['Country']更改为just item时,我会获得所选国家的所有信息,但当我将_mySelection=newVal更改为_mySelection=newVal['Country']时,我会得到一个错误 There should be exactly one item with [DropdownButton]'s value: Australia. Either ze

我正在制作一个corona应用程序,如果你从下拉列表中选择一个国家,它会显示感染者、死亡者和康复者的数量。 当我将值项['Country']更改为just item时,我会获得所选国家的所有信息,但当我将_mySelection=newVal更改为_mySelection=newVal['Country']时,我会得到一个错误

There should be exactly one item with [DropdownButton]'s value: Australia. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 827 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'
Statresbody示例 Countries包含阿富汗在本例中拥有相同信息的所有国家

{
    "Global": {
        "NewConfirmed": 174744,
        "TotalConfirmed": 8764876,
        "NewDeaths": 6071,
        "TotalDeaths": 468396,
        "NewRecovered": 90678,
        "TotalRecovered": 4245126
    },
    "Countries": [
        {
            "Country": "Afghanistan",
            "CountryCode": "AF",
            "Slug": "afghanistan",
            "NewConfirmed": 346,
            "TotalConfirmed": 27878,
            "NewDeaths": 2,
            "TotalDeaths": 548,
            "NewRecovered": 302,
            "TotalRecovered": 7962,
            "Date": "2020-06-20T19:19:28Z"
        },
        

     
      
您可以创建一个Country类这是您的CountryCollection类,如下所示:

class Country {
  String country;
  String countryCode;
  String slug;
  int newConfirmed;
  int totalConfirmed;
  int newDeaths;
  int totalDeaths;
  int newRecovered;
  int totalRecovered;
  String date;

  Country(
      {this.country,
      this.countryCode,
      this.slug,
      this.newConfirmed,
      this.totalConfirmed,
      this.newDeaths,
      this.totalDeaths,
      this.newRecovered,
      this.totalRecovered,
      this.date});

  Country.fromJson(Map<String, dynamic> json) {
    country = json['Country'];
    countryCode = json['CountryCode'];
    slug = json['Slug'];
    newConfirmed = json['NewConfirmed'];
    totalConfirmed = json['TotalConfirmed'];
    newDeaths = json['NewDeaths'];
    totalDeaths = json['TotalDeaths'];
    newRecovered = json['NewRecovered'];
    totalRecovered = json['TotalRecovered'];
    date = json['Date'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['Country'] = this.country;
    data['CountryCode'] = this.countryCode;
    data['Slug'] = this.slug;
    data['NewConfirmed'] = this.newConfirmed;
    data['TotalConfirmed'] = this.totalConfirmed;
    data['NewDeaths'] = this.newDeaths;
    data['TotalDeaths'] = this.totalDeaths;
    data['NewRecovered'] = this.newRecovered;
    data['TotalRecovered'] = this.totalRecovered;
    data['Date'] = this.date;
    return data;
  }
}
然后尝试创建下拉按钮,如下所示:

要填写您的国家列表,您可以迭代每个国家的元素,并使用Country类的fromJson方法,然后使用setState将元素添加到列表中。

我想说,使用国家代码作为下拉列表的值更为自然。 执行onChanged时,您可以从数据中获取完整的国家信息。 您的代码将被以下代码替换

    child: new DropdownButton(
      hint: Text(Country, style: TextStyle(fontSize: 18, color: Colors.black),
      ),
      isExpanded: true,
      items: data.map((item) {
        return new DropdownMenuItem(
          child: new Text(item['Country']),
          value: item['CountryCode'], // <===== use country code as value
        );
      }).toList(),
      onChanged: (newVal) {
        setState(() {
          _mySelection = newVal;
          // I get full country info
          var country = data.firstWhere((i) => i['CountryCode'] == newVal);
          // do what you want with the selection....
        });
      },
      value: _mySelection,
    ),

看起来澳大利亚的数据是多次的,每个国家都是如此,我几乎100%确定数据中只有一次。你好,西蒙,谢谢你的回答!我试着使用你的答案,但我想知道什么类型是selectedcountry,当我测试时,我似乎无法获得countries.mapSimon我想感谢你的帮助和你付出的努力,你的精彩!不客气。不要犹豫,把答案标记为正确的,以便其他人在需要时找到答案
child: new DropdownButton<Country>(
    value: selectedCountry,
    onChanged: (Country newValue) {
      setState(() {
        selectedCountry = newValue;
      });
    },
    items: countries.map((Country country) {
      return new DropdownMenuItem<Country>(
        value: country,
        child: new Text(
          country.country,
        ),
      );
    }).toList(),
  
    child: new DropdownButton(
      hint: Text(Country, style: TextStyle(fontSize: 18, color: Colors.black),
      ),
      isExpanded: true,
      items: data.map((item) {
        return new DropdownMenuItem(
          child: new Text(item['Country']),
          value: item['CountryCode'], // <===== use country code as value
        );
      }).toList(),
      onChanged: (newVal) {
        setState(() {
          _mySelection = newVal;
          // I get full country info
          var country = data.firstWhere((i) => i['CountryCode'] == newVal);
          // do what you want with the selection....
        });
      },
      value: _mySelection,
    ),