简体   繁体   English

为什么此下拉列表在 flutter 中失败?

[英]Why is this Dropdown failing in flutter?

Here is the code in my build method这是我的构建方法中的代码

Column(
                    children: [
                      DropdownButton<String>(
                          value: dropdownvalue,
                          items: <String>['A', 'B', 'C', 'D']
                              .map((String value) {
                            return DropdownMenuItem<String>(
                              value: value,
                              child: Text(value),
                            );
                          }).toList(),
                          onChanged: (String? newValue) {
                            setState(() {
                              dropdownvalue = newValue!;
                            });
                          }),
                      DropdownButton<String>(
                          value: dropdownvalue,
                          items: <String>['E', 'F', 'G', 'H']
                              .map((String value) {
                            return DropdownMenuItem<String>(
                              value: value,
                              child: Text(value),
                            );
                          }).toList(),
                          onChanged: (String? newValue) {
                            setState(() {
                              dropdownvalue = newValue!;
                            });
                          }),
                    ],
                  )

I have String dropdownvalue = '--';我有 String dropdownvalue = '--'; in my State class在我的 State class

The error I get is:我得到的错误是:

There should be exactly one item with [DropdownButton]'s value: --.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 894 pos 15: 'items == null || items.isEmpty || value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

I'm just trying to have multiple independent drop downs on my screen.我只是想在我的屏幕上有多个独立的下拉菜单。

Thanks谢谢

You should have a value per dropdown in order to store the selected value for each, instead of one for all of them.每个下拉列表应该有一个值,以便为每个下拉列表存储选定的值,而不是为所有下拉列表存储一个值。 So you should have a dropdownvalue1 for the first dropdown, dropdownvalue2 for the second, etc. Also the default values on your dropdownvalue properties should be among the ones in the list of values fed to each dropdown, therefore you should implement it as follows:因此,您应该为第一个下拉菜单设置一个dropdownvalue1 ,为第二个下拉菜单设置一个 dropdownvalue2,等等。此外,您的 dropdownvalue 属性的默认值应该在提供给每个下拉菜单的值列表中,因此您应该按如下方式实现它:


  String dropdownvalue1 = '--';
  String dropdownvalue2 = '--';
  
  @override
  Widget build(BuildContext context) {
    return Column(
        children: [
          DropdownButton<String>(
              value: dropdownvalue1,
              items: <String>['--','A', 'B', 'C', 'D']
                  .map((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
              onChanged: (String? newValue) {
                setState(() {
                  dropdownvalue1 = newValue!;
                });
              }),
          DropdownButton<String>(
              value: dropdownvalue2,
              items: <String>['--','E', 'F', 'G', 'H']
                  .map((String value) {
                return DropdownMenuItem<String>(
                  value: value,
                  child: Text(value),
                );
              }).toList(),
              onChanged: (String? newValue) {
                setState(() {
                  dropdownvalue2 = newValue!;
                });
              }),
        ],
      );
  }
}

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

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