【问题标题】:There should be exactly one item with Dropdownbutton's value应该只有一项具有 Dropdownbutton 的值
【发布时间】:2023-01-17 17:27:02
【问题描述】:

我是 flutter 的初学者,我正在尝试放置 4 个 dropdownbutton ,当我更改第二个的值时,出现错误:

[DropdownButton] 的值应该只有一项:Dollars。 检测到具有相同值的零个或两个或多个 [DropdownMenuItem] '包:dropdown_button2/dropdown_button2.dart': 包:dropdown_button2/dropdown_button2.dart:1 断言失败:第 1128 行第 11 行:'items == null || 项目.isEmpty || 价值==空|| items.where((DropdownMenuItem 项目) { 返回 item.value == 值; }).长度== 1'

第一个工作:

String lang = 'English';
 var items = [
   'English',
   'Polish',
 ];
DropdownButtonHideUnderline(
             child: DropdownButton2(
               items: items
                   .map(
                     (item) => DropdownMenuItem<String>(
                       value: item,
                       child: Text(
                         item,
                         style: const TextStyle(
                           fontSize: 14,
                         ),
                       ),
                     ),
                   )
                   .toList(),
               value: lang,
               onChanged: (String? newValue) {
                 setState(() {
                   lang = newValue!;
                 });
               },
               
             ),
           ),

第二个不是

String currency = 'Dollars';
  var curriencies = [
    'Dollar',
    'Euro',
    'PLN',
    'Funts',
  ];
DropdownButtonHideUnderline(
                    child: DropdownButtonFormField2(
                      items: curriencies
                          .map(
                            (currency) => DropdownMenuItem<String>(
                              value: currency,
                              child: Text(
                                currency,
                                style: const TextStyle(
                                  fontSize: 14,
                                ),
                              ),
                            ),
                          )
                          .toList(),
                      value: currency,
                      onChanged: (String? newValue) {
                        setState(() {
                          currency = newValue!;
                        });
                      },

我更改了值,但它仍然相同

【问题讨论】:

  • 什么是价值:货币?
  • 抱歉,我的错是它切断了“String currency = 'Dollars';”这一行,我现在已经编辑了
  • 将 Dollars 更改为 Dollars,因为此数组中不存在 Dollars - [ 'Dollar', 'Euro', 'PLN', 'Funts', ]

标签: flutter


【解决方案1】:

您收到该错误是因为在 Flutter 下拉菜单中,当前值必须是当前下拉选项列表中的第一个值。

因此,您的下拉列表中的第一个值应该是“美元”,您的可变货币也应该是“美元”。

【讨论】:

    【解决方案2】:

    正如错误提示Either zero or 2 or more [DropdownMenuItem]s were detected with the same value。货币变量的值是美元,货币列表中没有美元,因此它给出了一个空值错误。将货币值从美元更改为美元:-

    String currency = 'Dollar';
    

    【讨论】:

    • String currency = 'Dollar'; var curriencies = [ 'Dollar', 'Euro', 'PLN', 'Funts', ]; 我改变了,但仍然得到同样的东西:/
    • 热重启应用程序并查看其是否正常工作
    猜你喜欢
    • 2021-05-01
    • 1970-01-01
    • 2021-08-29
    • 2020-06-16
    • 1970-01-01
    • 2020-07-18
    • 2020-08-26
    • 2021-12-03
    • 2021-11-05
    相关资源
    最近更新 更多