I wrote a function which returns true if an element is present in a list. I created an earlier version in which the list contained "ints" and it worked fine. Now I have a list of "complex" objects, and it seems it never detects if the object is present. Here is the code :
bool addNewWordToBufferCarnetList(int? wordId) {
CarnetWords newWord =
CarnetWords(iD: wordId!, wrong: 0, right: 0, mastery: 0);
if (_bufferCarnetList.contains(newWord) == true) {
return false;
} else {
_bufferCarnetList.add(newWord);
notifyListeners();
return true;
}
}
Is there another way to see if the list contains the object ?
List.containsmethod documentation - it tells what you need to do to use it correctly==operator is wrong in your class, if in doubts check how other classes implement itRectobjects for example, you can useList.containsbeacauseRectclass implements==operator, like this: api.flutter.dev/flutter/dart-ui/Rect/operator_equals.html - go to the bottom of this page and you will see the implementation, so what you need to do is to implement==operator in yourCarnetWordsclass