错误:检测到零个或两个或两个以上具有相同值I/flutter(18363)的[DropdownMenuItem]:'package:flutter/src/material/dropdown.dart': [英] Error: Either zero or 2 or more [DropdownMenuItem]s were detected with the same value I/flutter (18363): 'package:flutter/src/material/dropdown.dart':

查看:98
本文介绍了错误:检测到零个或两个或两个以上具有相同值I/flutter(18363)的[DropdownMenuItem]:'package:flutter/src/material/dropdown.dart':的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误代码 我是新手,对dropdownbutton有一个疑问,希望将多个dropdownbutton使用相同的值.

Error code Hi I'm new to flutter and have a question about dropdownbutton regarding using the same values for multiple dropdownbutton.

据我对错误的理解,这是由于在同一活动中为2个或更多下拉按钮使用了相同的列表.

From my understanding from the error, it was due to using the same list for 2 or more dropdownbuttons in the same activity.

我如何解决此错误,但仍然可以将列表重新用于2个或更多下拉按钮?

How am i able to resolve this error but still able to reuse the list for 2 or more dropdownbuttons?

  String _value1;
  String _value2;

  final List<String> nameList = <String>[
    "Name1",
    "Name2",
    "Name3",
    "Name4",
    "Name5",
    "Name6",
    "Name7",
    "Name8"
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 2.0,
        title: Text('Hello'),
      ),
      body:  ListView(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Text('Name: '),
                  Center(
                    child: DropdownButton(
                      value: _value1,
                      onChanged: (value) {
                        setState(() {
                          _value1 = value;
                        });
                      },
                      items: nameList.map(
                        (item) {
                          return DropdownMenuItem(
                            value: item,
                            child: new Text(item),
                          );
                        },
                      ).toList(),
                    ),
                  ),
                ],
              ),
              Row(
                children: <Widget>[
                  Text('Name: '),
                  Center(
                    child: DropdownButton(
                      value: _value2,
                      onChanged: (value) {
                        setState(() {
                          _value2 = value;
                        });
                      },
                      items: nameList.map(
                        (item) {
                          return DropdownMenuItem(
                            value: item,
                            child: new Text(item),
                          );
                        },
                      ).toList(),
                    ),
                  ),
                ],
              ),
            ],
          ),
      backgroundColor: Colors.grey[200],
    );
  }
}

推荐答案

我遇到了完全相同的错误,多个Dropdowns都从同一个静态列表中获取数据,唯一的区别是,在我的情况下,这是一个Objects列表,不是字符串.

I had the exact same error, multiple Dropdowns all feeding from the same static list, the only difference is that in my case, it was a list of Objects, not Strings.

因此,如果它是静态列表,则不可能为空,列表中没有重复的值,并且您已经确定value不为空?然后剩下的唯一选择是item.valuevalue

So, if it's a static list, there's no way it's empty, no duplicate values in the list, AND you already make sure value is not empty? Then the only option remaining is that item.value is different than value

就我而言,因为它是一个对象列表,所以我不得不覆盖Object类中的operator ==hashcode方法.

In my case, as it was an Object list, I had to overwrite operator == and hashcode methods in my Object class.

bool operator ==(dynamic other) =>
      other != null && other is TimeSelection && this.hour == other.hour;

  @override
  int get hashCode => super.hashCode;

就是这样.我不必初始化_value1_value2

And that was it. I didn't had to initialize _value1 or _value2

这篇关于错误:检测到零个或两个或两个以上具有相同值I/flutter(18363)的[DropdownMenuItem]:'package:flutter/src/material/dropdown.dart':的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆