Object Equality in Dart and Flutter

Gautier Siclon
Gautier Siclon

Co-founder, Apparence.io

Flutter tips · Published on

To the Dart runtime, two separate instances of the same class are not equal by default, even if all their data is identical. This behavior can lead to subtle bugs in your app's user interface updates and overall logic.

The Problem: Referential Equality

By default, Dart compares objects based on their memory address. Think of it this way: if you create two 'Task' objects that look identical (e.g., both have 'title: Buy groceries' and 'description: Milk and eggs'), Dart still sees them as two completely different things because they exist at different spots in your computer's memory. This is called referential equality.

Achieving Value Equality

Sometimes, you want objects to be considered equal if their values are the same, regardless of where they are stored in memory. This is known as value equality. To make this happen, you have two main approaches in Dart:

1. Overriding the == Operator and hashCode Method

To compare objects based on their values, you need to tell Dart how to do it. You do this by overriding the == operator and the hashCode getter within your class. The == operator defines when two objects are considered equal. The hashCode is a special number that helps Dart quickly tell if two objects might be equal, which is crucial for collections like Sets and Maps.

How it works:

  • == Operator: You implement logic to check if all relevant properties of two objects are the same.
  • hashCode Getter: You combine the hash codes of the relevant properties to create a unique hash for your object. If two objects are equal, their hash codes must be the same.

2. Using the equatable Package

For a simpler and less error-prone way to implement value equality, you can use the popular equatable package. This package helps you avoid writing the boilerplate code for == and hashCode manually.

How it works:

  • You extend your class with Equatable.
  • You override the props getter and provide a list of all the properties that should be considered when checking for equality. The equatable package then handles the == and hashCode generation for you based on these properties.

Why This Matters

Implementing value equality is vital for many reasons, especially in Flutter development. It ensures that your UI updates correctly when an object's data changes, even if the object itself is a new instance. It also helps in scenarios where you're comparing lists, filtering data, or using objects as keys in maps, ensuring your application logic behaves as expected.

Save 3 months of work

One command. Pick your modules. Firebase or Supabase auto-configured. Start building what matters.

kickstarter for flutter apps

Frequently Asked Questions

What is referential equality in Dart?

Referential equality means Dart compares objects based on their unique memory location, not their internal data. Two objects with identical data will be considered different if they are distinct instances.

Why is default object equality a problem in Flutter apps?

If you expect objects with the same data to be equal, the default referential equality can cause problems. For example, UI elements might not update correctly, or your app's logic could have subtle bugs when checking if two 'identical' items are the same.

How can I compare Dart objects by their values?

You can achieve value equality by either manually overriding the `==` operator and `hashCode` method in your class, or by using the `equatable` package, which simplifies this process.

What does the `equatable` package do?

The `equatable` package provides a convenient way to implement value equality. By extending `Equatable` and listing the relevant properties in the `props` getter, it automatically generates the `==` operator and `hashCode` for you.

When should I use value equality for my objects?

You should use value equality when you want two separate object instances with the exact same data to be treated as equal. This is especially important for UI updates, working with collections like `Set`s or `Map`s, and ensuring consistent application logic.

Read more
You may also be interested in
Made by ApparenceKit logo
ApparenceKit is a flutter start kit | template generator tool by Apparence.io © 2026.
All rights reserved