How to compare objects in Dart?

Walid Cena
3 min readAug 14, 2023

--

Object Comparison refers to the process of determining whether two objects are equal or not in a programming context. It’s a fundamental operation in software development and is used to compare the contents or attributes of two objects to ascertain whether they represent the same data or have the same state.

Object comparison is used for a variety of purposes, including:

  1. Data Validation: When receiving data from external sources, like user inputs or databases, you often need to compare objects to validate if they match expected values or conditions.

2. Duplication Detection: In scenarios where you’re dealing with collections of objects, you might want to detect if any objects are duplicated, i.e., two things representing the same data exist.

3. Equality Testing: Object comparison is essential when you want to test if two objects have the same values, attributes, or state. This is especially important when working with complex data structures.

4. Searching and Filtering: When searching for a specific object in a collection or database, you need to compare things to find the desired match.

5. User Interfaces: In graphical user interfaces, object comparison is used to determine whether the current state of an object is different from its previous state, helping to decide whether updates or redraws are necessary.

6. Caching and Memoization: In optimization techniques, you can use object comparison to determine if a particular computation or operation has been performed before. If so, you can reuse the result instead of re-computing it.

7. Hash-Based Data Structures: Many hash-based data structures, like hash tables and hash maps, require the use of object comparison and hashing functions to efficiently store, retrieve, and manipulate data.

The concept of object comparison is closely tied to the idea of equality. Objects can be considered equal based on various criteria, such as structural equality (the values of their attributes), reference equality (pointing to the exact memory location), or some customized comparison logic you define for your classes.

In programming languages, you often can customize object comparison behavior for user-defined classes by overriding methods like `equals` (Java) or `==` and `hashCode` (Python, Dart, etc.). This customization allows you to define what it means for objects of your class to be equal, enhancing the flexibility and accuracy of your comparisons.

So how to compare objects in Dart

let's say we have a car class like this :

class Car {
final String make;
final String model;
final int year;

Car({
required this.make,
required this.model,
required this.year,
});

@override
bool operator ==(covariant Car other) =>
identical(this, other) ||
(make == other.make && model == other.model && year == other.year);

@override
int get hashCode => Object.hash(make, model, year);
}

Now it's time to make 3 objects and test

  Car car1 = Car(make: "BMW", model: "X5", year: 2022);
Car car2 = Car(make: "Mercedes", model: "C-Class", year: 2023);
Car car3 = Car(make: "BMW", model: "X5", year: 2022);

let's compare objects and see which two have the same data

  // Compare car1 and car2
if (car1 == car2) {
print("car1 is the same as car2");
} else {
print("car1 is not the same as car2");
}

// Compare car1 and car3
if (car1 == car3) {
print("car1 is the same as car3");
} else {
print("car1 is not the same as car3");
}
// Output will be
// car1 is not the same as car2
// car1 is not the same as car3

that's it.

--

--