3

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 ?

8
  • read carefully List.contains method documentation - it tells what you need to do to use it correctly Commented May 29, 2021 at 13:13
  • Hmm, I have read it several times... but I don't see what I am doing wrong. Is it because it is now a complex object ? Should I just use a for loop and do it "manually" ? Commented May 29, 2021 at 13:19
  • they say: "The equality used to determine whether element is equal to an element of the iterable defaults to the Object.== of the element." - so == operator is wrong in your class, if in doubts check how other classes implement it Commented May 29, 2021 at 13:22
  • I guess I don't understand their sentence... am unfortunately a beginner and not a native English speaker... So can the List.contains method be used in my case ? Commented May 29, 2021 at 13:23
  • if you have a list of Rect objects for example, you can use List.contains beacause Rect class 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 your CarnetWords class Commented May 29, 2021 at 13:28

1 Answer 1

2

As suggested and explained by pskink, I had to override the == operator in my CarnetWords class for the "equality" to function. Here is the code :

class CarnetWords {
 @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    if (runtimeType != other.runtimeType) return false;
    return other is CarnetWords && other.iD == iD;
  }
 @override
  int get hashCode => iD.hashCode;

  int? iD;
  int? wrong;
  int? right;
  double? mastery;
  CarnetWords(
      {@required this.iD,
      @required this.wrong,
      @required this.right,
      @required this.mastery});
}

Thanks pskink for your help and patience !

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

4 Comments

Hello again :) Are you online ?
I had a crazy pb with Textfield. I don't know if you can see my post from here. I managed to find a way around it, but I still don't understand why it didn't work...
i dont see any Textfield in the code you posted

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.