How do I compare two objects to see if they are the same instance, in Dart?

You’re looking for “identical”, which will check if 2 instances are the same.

identical(this, other);

A more detailed example?

class Person {
  String ssn;
  String name;


  // Define that two persons are equal if their SSNs are equal
  bool operator ==(Person other) {
    return (other.ssn == ssn);

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

  print(bob == robert); // true

  print(identical(bob, robert)); // false, because these are two different instances

Leave a Comment