I use the following code to compare two Person object using the overridden == operator:

class Person {
  String ssn;
  String name;

  Person(this.ssn, this.name);

  bool operator ==(Object other) {
    return identical(this, other) ||
      other is Person && 
      ssn == other.ssn &&
      name == other.name;
  }

  @override int get hashCode => (ssn + name).hashCode;
}

main() {
  var bob =  Person('111', 'Bob');
  var robert =  Person('123', 'Robert');

  print(bob == robert); // false
}

However, as this code works perfectly with Person objects, is there a way to write one == operator function that works for any two types to compare them to check if they are equal or not ?


Solution 1: ChessMax

If your question is how not to write equals and hashCode implementation for all your classes then the answer would be there is no way right now. But you can simplify this process with packages. For example, you can use Equatable package then your class would look like this:

import 'package:equatable/equatable.dart';

class Person extends Equatable {
  String ssn;
  String name;

  Person(this.ssn, this.name);

  @override
  List<Object> get props => [name, ssn];
}