繁体   English   中英

如何解决 flutter 中的“[DropdownMenuItem]s were detected with the same value”错误?

[英]How to resolve this error "[DropdownMenuItem]s were detected with the same value" in flutter?

用户界面

在此处输入图像描述

在我的代码中,当两个参数在国家/地区下拉列表中时

在此处输入图像描述

when selected India, then show Indian provinces. Also, when selected USA, then show USA provinces in the province dropdown the way, after selecting a province, and again when changing the country, then show an error.

在此处输入图像描述

当我想要显示警告消息的那种情况或者当更改国家然后省份应该更改初始值时如何解决此错误。

代码


class _HomePageState extends State<HomePage> {
  List<String> countries = ['USA', 'India'];
  List<String> indiaProvince = ['New Delhi', 'Bihar', 'Rajasthan'];
  List<String> usaProvince = ['Texas', 'Florida', 'California'];

  List<String> provinces = [];
  String? selectedCountry;
  String? selectedProvince;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Multi Level Dropdown'),
      ),
      body: ListView(
        padding: EdgeInsets.all(20.0),
        children: [
          // Country Dropdown
          DropdownButton<String>(
            hint: Text('Country'),
            value: selectedCountry,
            isExpanded: true,
            items: countries.map((String value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(value),
              );
            }).toList(),
            onChanged: (country) {
              if (country == 'USA') {
                provinces = usaProvince;
              } else if (country == 'India') {
                provinces = indiaProvince;
              } else {
                provinces = [];
              }
              setState(() {
                selectedProvince != 'null';
                selectedCountry = country;
              });
            },
          ),
          // Country Dropdown Ends here

          // Province Dropdown
          DropdownButton<String>(
            hint: Text('Province'),
            value: selectedProvince,
            isExpanded: true,
            items: provinces.map((String value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(value),
              );
            }).toList(),
            onChanged: (province) {
              setState(() {
                selectedProvince = province;
              });
            },
          ),
          // Province Dropdown Ends here
        ],
      ),
    );
  }
}

您的下拉列表中存在重复的值,您需要使用某种方式排除重复,我建议使用toSet() ,而不是这部分代码:

items: countries.map((String value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(value),
              );

将其更改为:

items: countries.toSet().toList().map((String value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(value),
              );

然后再次尝试运行

您的问题是每次国家更改时您将indiaProvinceusaProvince推入provinces ,当您两次更改国家/地区时,您将两次将indiaProvince推入provinces ,因此将您的国家/地区的 onChanged 更改为:

onChanged: (country) {
  if (country == 'USA') {
    provinces = []; // <--- add this
    provinces = usaProvince;
  } else if (country == 'India') {
    provinces = []; // <--- add this
    provinces = indiaProvince;
  } else {
    provinces = [];
  }
  setState(() {
    selectedProvince = null; // <--- change this
    selectedCountry = country;
  });
},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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