Home > database >  Adding and removing objects from a list in flutter
Adding and removing objects from a list in flutter

Time:10-03

I have List of type dynamic called selectedList, it should hold a list of objects. each objects consists of teacherId and subjectId, Like this [ {1,2},{4,9},{7,3} ... etc]

what I want to achieve is that I can remove and add objects to this list, if the object is exist it should be removed and if it's not it should be added.

I tried contain method but it didn't work !

The Adding method

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);}

where I'm calling the method

                                      ListTile(
                                             

onTap: () { card.addTeacher( e, cardData.subjects[ subjectIndex]); },

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

Please Help I'm really tired of this problem!

CodePudding user response:

As you mentioned Object and since contains method is not working

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

Step 1

I suppose you are using contains for objects without overriding their hashcode and equallity operator

eg:

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;
}

Step 2

Since you are using Set in the list, you have to comapre them in a different way

Also remove this keyword, it's not nessecerly

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 method will return -1 if the element was not found in the list. you can use other methods like List().every too

(Instead of Set, I recommend you to use Map for code readabiliy and reduce complexity)

  • Related