Search code examples
flutterdartequals

Is it possible to use .equals() method like Java in Dart


I'm a Java developer and the newbie in Dart.

When I compare two objects in Dart, it only has == operator which helps me compare two logical memory addresses of two objects in Dart. How can I compare two objects to be the same like Java without be preparing this code below? It makes me tired to prepare entity's class so I wonder that have any Dart's ways for that?

class MyClass {
    final MySubClass mySubClass;

    MyClass({this.mySubClass});

    bool equals(Object other) => identical(this, other) || other is MyClass && runtimeType == other.runtimeType && something.equals(other.mySubClass);
}

class MySubClass {
    final String something;

    MySubClass({this.something});

    bool equals(Object other) => identical(this, other) || other is MySubClass && runtimeType == other.runtimeType && something == other.something;
}

Solution

  • Dart language don't have equals method. According this article, I have to use Equatable Package.