简体   繁体   English

DropdownButton 在 Flutter 中无法正常工作

[英]DropdownButton not working as expected in Flutter

Am new to Flutter Development.我是 Flutter 开发的新手。 Am populating a snapshot data received from API in DropdownButtonlist.在 DropdownButtonlist 中填充从 API 收到的快照数据。 everything works fine.一切正常。 but when I change the items in the list I get above error.但是当我更改列表中的项目时,出现上述错误。 am not sure which one is causing the pbm.我不确定是哪一个导致了 pbm。 i surfed the.net a lot but could not find the solution.我在 .net 上冲浪了很多次,但找不到解决方案。 I get the error even if I have one item in the list.即使列表中有一项,我也会收到错误消息。 I get error "Error: Either zero or 2 or more [DropdownMenuItem]s were detected with the same value in flutter " Thanks in advance我收到错误消息“错误:在 flutter 中检测到零个或 2 个或更多 [DropdownMenuItem] 具有相同的值”提前致谢

FutureBuilder(
              future:Api.getSchemes(context),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                return snapshot.hasData
                    ? Container(
                  padding: EdgeInsets.all(5),
                    decoration: BoxDecoration(
                      color: Color(0xffF3D876),
                      borderRadius: BorderRadius.circular(10),
                    ),

                  child: DropdownButton<ClsSchemes>(
                    alignment: AlignmentDirectional.centerStart,
                    isExpanded: true,
                    value: dropDownValue,
                    hint: Text(dropDownValue.Scheme_Name ?? 'Make a selection'),
                    items: snapshot.data.map<DropdownMenuItem<ClsSchemes>>((item) {
                      return DropdownMenuItem<ClsSchemes>(

                        value: item,
                        child: Text(item.Scheme_Name),
                      );
                    }).toList(),
                    onChanged: (value) {
                      setState(() {
                        dropDownValue = value!;
                        TotalDues = value.Total_Dues;
                      });
                    },
                  ),
                )
                    : Container(
                  child: Center(
                    child: Text('Loading...'),
                  ),
                );
              },
            ),

Since you are using an instance of ClsSchemes as your value, you need to make sure that operator == actually works properly for your class.由于您使用ClsSchemes的一个实例作为您的值,因此您需要确保operator ==实际上适用于您的 class。

For this you need to override the == operator and the hashCode :为此,您需要覆盖==运算符hashCode

Example take from here例子取自 这里

class Example {
  final int value;
  Example(this.value);

  @override
  bool operator ==(Object other) =>
      other is Example &&
      other.runtimeType == runtimeType &&
      other.value == value;

  @override
  int get hashCode => value.hashCode;
}

You need to find out what the identifying value of your class is.您需要找出您的 class 的标识值是多少。 When are two of those classes equal?这些类中的两个什么时候相等?

If you already have specific fields that are your "identity" fields, the package equatable makes it a little easier.如果您已经有作为“身份”字段的特定字段,则package可等式会使它更容易一些。

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

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