Compare dart object

Flutter tips Published on

Understanding Equals and Hashcode

When working with Dart objects, you sometimes need to check if two objects are the same or represent the same value. Dart provides the == operator for equality checks and a hashCode property.

By default, == checks if two variables refer to the exact same instance in memory. The default hashCode is based on the instance identity.

However, for many custom classes, you want == to check if two different instances have the same value (like having the same name for a Person object).

Why Overriding is Important

A hash code is a number linked to each object. It's super useful for data structures like HashMap and HashSet. These structures use the hash code to quickly find objects.

If you override == to check for value equality but don't override hashCode, things can go wrong. Objects that you consider equal by your == logic might end up in different places in a HashMap because they have different default hash codes. This breaks the contract that if two objects are equal (a == b is true), their hash codes must be the same (a.hashCode == b.hashCode).

So, to compare two instances of a class like Person based on their properties (like name), you need to override both == and hashCode.

Example: The Person Class

Here's how you might override == and hashCode for a simple Person class:

class Person {
  const Person(this.name);

  final String name;

  
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Person &&
      runtimeType == other.runtimeType &&
      name == other.name;

  
  int get hashCode => name.hashCode;
}

In this example, two Person objects are considered equal if they are the exact same instance (identical(this, other)) OR if the other object is also a Person, has the same type, and their name properties are the same.

The hashCode is calculated simply using the hash code of the name property.

How to Generate it Easily

Writing these overrides manually can be tedious. Luckily, there are easier ways:

Using one of these methods makes handling object comparison much simpler and less error-prone.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

Why do I need to override == and hashCode in Dart?

You need to override both if you want two different instances of your class to be considered equal based on their property values, rather than just being the exact same instance in memory. This is crucial for collections like HashMaps.

What is hashCode used for?

The hashCode is an integer value used by hash-based data structures like HashMaps and HashSets to quickly store and retrieve objects. It helps them determine where an object should be placed or looked up.

What happens if I only override == and not hashCode?

If you override `==` for value equality but not `hashCode`, equal objects might have different hash codes. This breaks the required contract and can cause issues when using these objects in hash-based collections, where they might not be found correctly.

Is there an easy way to generate == and hashCode?

Yes, you can use packages like `equatable` or `freezed`. Many IDEs like IntelliJ and Android Studio also have code generation features to do this for you.

Read more
You may also be interested in
Create an image from widget  blog card image
Create an image from widget
Published on 2025-05-12T09:27:21.320Z
Listen keyboard visibility state  blog card image
Listen keyboard visibility state
Published on 2025-05-12T09:22:43.829Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved