Skip to content
Blog

Understanding Equality in Dart: ==, identical(), and the hashCode Contract

How Dart's == really works, when identical() beats it, how const canonicalization surprises you, and the rules for overriding == and hashCode together.

Published on • December 3, 2026

AI Assistant

Two Dart values can look identical and still be different objects — or look different and compare equal. Keeping ==, identical(), and hashCode straight is the difference between a Map that works and a lookup that silently returns null.

Two questions: same value, or same object?

Every Dart object has an identity that separates it from all other objects, even ones with identical state, and the default == inherited from Object is identity equality — which is why two fresh instances of a plain class compare unequal until you override the operator. That gives you two distinct questions to ask about any pair of values:

  • Same value? Use ==. By default it means identity, but any class can redefine it to mean something else, such as two Money instances with the same amount and currency.
  • Same object? Use identical(a, b). No class can redefine this; it inspects the actual references.

How == actually works

The == operator follows a two-step rule defined by the language:

  1. If either operand is null, return true only when both are null.
  2. Otherwise, invoke the == method on the left operand with the right operand as the argument.
void main() {
  Object? a = null;
  Object? b = null;
  Object c = 'text';
  print(a == b); // true — both null, no method call needed
  print(a == c); // false — exactly one side is null
  print(c == 'text'); // true — String's == compares code units
}

Step two is the important one: operators are methods dispatched on their leftmost operand, the same rule that governs every overloaded operator discussed in dart-operators-arithmetic-logical-null-aware.md. It also means null == x never calls a method on null, which is why your own == implementation does not need to handle a null argument.

identical() and canonicalization

identical(a, b) reports whether two references point at the same object. The Dart API documentation highlights the two behaviors that surprise people: non-constant constructors always make fresh objects, while compile-time constants with the same state are canonicalized into a single instance:

void main() {
  print(identical(const <int>[1], const <int>[1])); // true — const canonicalized
  print(identical([1], [1])); // false — two fresh objects
  print(identical(const [1], const [2])); // false — different state
  print(identical(2, 1 + 1)); // true — equal ints are the same object
  final list = [1, 2];
  print(identical(list, list)); // true — same reference
  print(identical(list, [1, 2])); // false — new list, equal contents
}

Integers and doubles with the same value are always identical because Dart does not allow creating new instances of them. Records are the opposite: they have no persistent identity at all, so identical(pair, pair2) may be true or false for two equal records depending on how the compiler chooses to represent them.

A recipe for value equality

Value equality is a three-part recipe: override ==, override hashCode alongside it, and build both from the same final fields:

class Money {
  final int amount;
  final String currency;
  const Money(this.amount, this.currency);
  @override
  bool operator ==(Object other) =>
      other is Money && other.amount == amount && other.currency == currency;
  @override
  int get hashCode => Object.hash(amount, currency);
}
void main() {
  const a = Money(100, 'THB');
  const b = Money(100, 'THB');
  final c = Money(100, 'THB');
  print(a == b); // true — value equality
  print(a == c); // true — even across const and runtime instances
  print(identical(a, b)); // true — both const, canonicalized together
  print(identical(a, c)); // false — different objects, equal values
  print({a, b, c}.length); // 1 — a Set deduplicates using == and hashCode
}

The parameter is Object, not Object?. Effective Dart explicitly says DON’T make the parameter to == nullable: the language guarantees the right-hand side is never null when your method is called, so the canonical form takes a plain Object parameter and opens with a type test such as other is Money.

The rules your == must obey

Effective Dart frames these as the mathematical rules of equality, and users have deep intuition about them whether you document them or not — comparisons must also stay consistent, returning the same answer for the same unmodified objects every time:

  • Reflexive: a == a is always true.
  • Symmetric: a == b returns the same as b == a.
  • Transitive: if a == b and b == c, then a == c.

Symmetry is the rule broken most often, usually by writing a type test that only accepts one side’s class. If a class cannot obey these laws, then == is simply the wrong name for the operation you want — pick a method with a descriptive name instead.

The hashCode contract

The matching rule for hashing is short: any two objects that are equal must have the same hash code. Hash-based collections depend on it, because a Map or Set only compares two keys with == after their hashCodes put them in the same bucket. Effective Dart makes this a hard rule with the hash_and_equals linter: DO override hashCode if you override ==.

class BadKey {
  final int id;
  BadKey(this.id);
  @override
  bool operator ==(Object other) => other is BadKey && id == other.id;
  // hashCode is missing — it stays an identity hash.
}
void main() {
  final map = <BadKey, String>{};
  map[BadKey(1)] = 'first';
  print(map[BadKey(1)]); // null — equal key, different hash bucket
}

The lookup fails even though the two keys compare equal with ==, because their identity hashCodes place them in different buckets. Adding int get hashCode => Object.hash(id); fixes it — the Object.hash helper combines field hashCodes correctly without you writing any bit twiddling.

When not to override equality

Two classes of value deserve the default identity equality. The first is mutable classes: if a field can change, then an object’s hashCode can change after it has been inserted into a set or map, and hash-based collections assume hashCodes are stable for an object’s lifetime. Effective Dart says AVOID defining custom equality for mutable classes, enforced by the avoid_equals_and_hash_code_on_mutable_classes linter. The second is double, where == follows IEEE 754 rather than intuition:

void main() {
  final a = 0.0 / 0.0;
  final b = 0.0 / 0.0;
  print(a == b); // false — NaN is not equal to itself
  print(a.isNaN); // true — the check you actually want
  print(0.0 == -0.0); // true — despite different bit patterns
}

Never use == to detect NaN; use the isNaN getter. And when you need reference semantics — cache checks, sentinel values, identity-based maps — identical() is the honest tool, as noted in the identical() API documentation.

== vs identical() at a glance

QuestionToolExampleTypical use
Same value?==Money(100, 'THB') == Money(100, 'THB')Business equality
Same object?identical()identical(list, list)Caches, sentinels, null checks
Same bucket?hashCodeObject.hash(amount, currency)Keys in Map and Set

Structural types come with equality built in: records compare field by field, and constant patterns in switch and if-case compare with ==, so overriding == quietly changes pattern-matching behavior too — see dart-patterns-and-records.md and dart-if-else-switch-statements.md for how patterns consume equality.

Takeaways

  • == asks about value equality and dispatches on the left operand after a null check; identical() asks whether two references are the same object.
  • Const expressions with equal state are canonicalized, so identical(const a, const b) can be true, while fresh constructor calls never are.
  • Override == and hashCode together, from the same final fields, using Object.hash(...) to combine them.
  • Obey reflexivity, symmetry, transitivity, and consistency, and take the Object (not Object?) parameter form.
  • Leave mutable classes and double on identity equality, and reach for isNaN instead of == with NaN.

Takeaway: Override == only for immutable value types, always override hashCode with it, and use identical() whenever the question is about object identity rather than value.

References