【问题标题】:Adding and removing objects from a list in flutter在颤动中从列表中添加和删除对象
【发布时间】:2021-11-24 02:42:24
【问题描述】:

我有一个动态类型的列表,称为 selectedList,它应该包含一个对象列表。 每个对象由teacherId和subjectId组成, 像这样 [ {1,2},{4,9},{7,3} ... 等]

我想要实现的是我可以删除对象并将其添加到此列表中,如果对象存在,则应将其删除,如果不存在,则应添加。

我尝试了包含方法,但没有成功!

添加方法

List<dynamic> selectedList = [];

void addTeacher(Teachers teacherId, Subjectt subjectId) {
if (this.selectedList.contains({  *** this method is not working***     })) {
  this.selectedList.remove({teacherId, subjectId});
  this.myColor = Colors.grey;
  print('removed teacher => ${teacherId.toString()}');
} else {
  this.selectedList.add({teacherId, subjectId});
  this.myColor = AsasColors().blue;
  print('added teacher => ${teacherId.toString()}');
}
notifyListeners();
print(selectedList);}

我在哪里调用方法

                                      ListTile(
                                             

onTap: () { 卡片.addTeacher( e, cardData.subjects[ 主题索引]); },

                                              leading: CircleAvatar(
                                                backgroundColor:
                                                    AsasColors().white,
                                                backgroundImage:
                                                    NetworkImage(
                                                        e.profilePicture),
                                              ),
                                              title: MediumRegularFont(
                                                string: e.name,
                                              ),),

请帮助我真的厌倦了这个问题!

【问题讨论】:

  • 您在添加或删除对象后调用 setState()?
  • 不,我在 changeNotifier 类中编写此方法,并在此方法末尾使用 notifyListeners() 并用 Consumer 包装我的列表图块

标签: list flutter dart mobile collections


【解决方案1】:

正如你提到的Object 并且由于contains 方法不起作用

...
if (this.selectedList.contains({  *** this method is not working***     })) ...

步骤 1

我想你正在为对象使用 contains 而没有覆盖它们的哈希码和相等运算符

例如:

class Teacher {
  String id;
  String name;

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
  
    return other is Teacher &&
      other.id == id;
  }

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

第二步

由于您在列表中使用Set,因此您必须以不同的方式对它们进行比较

同时删除this关键字,这不是nesecerly

List<Set> selectedList = [];

void addTeacher(Teachers teacherId, Subjectt subjectId) {
if (selectedList.indexWhere((element) => 
   element.first == teacherId && element.last == subjectId)) != -1 ) {
  selectedList.remove({teacherId, subjectId});
  myColor = Colors.grey;
  print('removed teacher => ${teacherId.toString()}');
} else {
  selectedList.add({teacherId, subjectId});
  myColor = AsasColors().blue;
  print('added teacher => ${teacherId.toString()}');
}
notifyListeners();
print(selectedList);
}

List().indexWhere 方法将返回 -1 如果在列表中找不到元素。您也可以使用其他方法,例如List().every

(我建议您使用Map 代替Set,以提高代码可读性并降低复杂性)

【讨论】:

  • 谢谢你的回答,我以前做过,但实际上我对这个文件很陌生,我不知道在 contains 方法中输入什么,我现在应该这样检查吗? if (this.selectedList.contains( {teacherId . id,subjectId}) ) ??
  • @FarisShomali 我已经更新了我的答案,我不知道你第一次使用套装
猜你喜欢
  • 2022-01-16
  • 1970-01-01
  • 2012-06-06
  • 2022-01-04
  • 1970-01-01
  • 2013-02-08
  • 2020-06-04
  • 1970-01-01
  • 2021-08-03
相关资源
最近更新 更多