How does a set determine that two objects are equal in dart?

For a comprehensive write-up about operator== in Dart see

It just checks if they are equal a == b You can override the == operator to customize this behavior. Keep in mind that also hashCode should be overridden when the == operator is overridden.

class Action {
  bool operator==(other) {
    // Dart ensures that operator== isn't called with null
    // if(other == null) {
    //   return false;
    // }
    if(other is! Action) {
      return false;
    return description == (other as Action).description;

  // hashCode must never change otherwise the value can't
  // be found anymore for example when used as key 
  // in hashMaps therefore we cache it after first creation.
  // If you want to combine more values in hashCode creation
  // see
  // This is just one attempt, depending on your requirements
  // different handling might be more appropriate.
  // As far as I am aware there is no correct answer for
  // objects where the members taking part of hashCode and
  // equality calculation are mutable.
  // See also
  int _hashCode;
  int get hashCode {
    if(_hashCode == null) {
      _hashCode = description.hashCode
    return _hashCode;
  // when the key (description) is immutable and the only
  // member of the key you can just use
  // int get hashCode => description.hashCode

try at DartPad

Leave a Comment