3

I have a list called selected list of type dynamic and it holds a list of objects, each objects contains from a teacher ID & index.

I want to check if this list contains the same Id & index, if it does i want to remove this object from the list.

here is my code ....

void addTeacher(int teacherId, int index) {

if (this.selectedList.contains({     **/// the problem is here**    })) {

  this.selectedList.remove({teacherId, index});
  this.myColor = Colors.grey;
  print('removed teacher => ${teacherId.toString()}');

} else {

  this.selectedList.add({teacherId, index});
  this.myColor = AsasColors().blue;
  print('added teacher => ${teacherId.toString()}');

}

notifyListeners();
print(selectedList);

}

how can i achive this ?

2 Answers 2

3

Contains and remove use the == operator which in this case will return false because unless you override it for a specific class it will compare by reference. You can use indexWhere to find out if an item is in a list based on a compare function like that (if the function returns -1 the item is not on the list:

// Index different than -1 means the item is found somewhere in the list
final teacherIndex = this.selectedList.indexWhere((teacher) => teacher['teacherId'] == teacherId);
if (teacherIndex != -1) {
  this.selectedList.removeAt(teacherIndex);
  this.myColor = Colors.grey;
  print('removed teacher => ${teacherId.toString()}');
} else {
  ...
}

Sign up to request clarification or add additional context in comments.

3 Comments

unfortunately, this is not working.
Refactored the access for the teacher set. Check it out now
it keeps giving me this error Class '_CompactLinkedHashSet<int>' has no instance method '[]'.
2

I have implemented it and it worked fine
[The code] [The output:]

This is the written code:

class Subject {
  int? teacherID;
  int? subjectID;
  Subject(this.teacherID, this.subjectID);

  @override
  String toString() => "Subject {teacherID: $teacherID, subjectID: $subjectID";

  //TODO: Change your needed criteria here..
  @override
  bool operator ==(Object other) =>
      other is Subject &&
      teacherID == other.teacherID &&
      subjectID == other.subjectID;
}

void addSubject(List<Subject> list, Subject subject) {
  if (list.contains(subject)) {
    list.remove(subject);
  } else {
    list.add(subject);
  }
}

void main() {
  List<Subject> selectedList =
      List.generate(10, (index) => Subject(index + 1, index + 1));

  print("SelectedList = $selectedList");

  addSubject(selectedList, Subject(11, 11));
  addSubject(selectedList, Subject(11, 12));
  addSubject(selectedList, Subject(12, 11));
  addSubject(selectedList, Subject(12, 12));
  print("SelectedList2 = $selectedList");

  addSubject(selectedList, Subject(12, 12));
  print("SelectedList3 = $selectedList");
}

Sincerely, accept the answer if it worked for you.

6 Comments

Please code in place of image : meta.stackoverflow.com/questions/285551/…
thanks for your help ahmed, adding objects to array works fine but deleting them didn't work, it keeps duplicating existing objects....
i think contains method is not working in my case
i have to lists not one, subjects list and teachers list , each subjects has id also each teacher has id, .... i want to add an object to list as this form [ {teacherId,subjectId} ], what have you provided not covering my case, thank you
also, the teachers list is inside the objects list , they are at the same response
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.