Viewing version 2.0.0. Open the latest package page.

equatable

2.0.0

A Dart package that helps to implement value based equality without needing to explicitly override == and hashCode.

Download archive

Readme


Overview

Being able to compare objects in Dart often involves having to override the == operator as well as hashCode.

Not only is it verbose and tedious, but failure to do so can lead to inefficient code which does not behave as we expect.

By default, == returns true if two objects are the same instance.

Let's say we have the following class:

class Person {
  const Person(this.name);

  final String name;
}

We can create create instances of Person like so:

void main() {
  final Person bob = Person("Bob");
}

Later if we try to compare two instances of Person either in our production code or in our tests we will run into a problem.

print(bob == Person("Bob")); // false

For more information about this, you can check out the official Dart Documentation.

In order to be able to compare two instances of Person we need to change our class to override == and hashCode like so:

class Person {
  const Person(this.name);

  final String name;

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

  @override
  int get hashCode => name.hashCode;
}

Now if we run the following code again:

print(bob == Person("Bob")); // true

it will be able to compare different instances of Person.

You can see how this can quickly become a hassle when dealing with complex classes. This is where Equatable comes in!

What does Equatable do?

Equatable overrides == and hashCode for you so you don't have to waste your time writing lots of boilerplate code.

There are other packages that will actually generate the boilerplate for you; however, you still have to run the code generation step which is not ideal.

With Equatable there is no code generation needed and we can focus more on writing amazing applications and less on mundane tasks.

Usage

First, we need to do add equatable to the dependencies of the pubspec.yaml

dependencies:
  equatable: ^2.0.0

Next, we need to install it:

# Dart
pub get

# Flutter
flutter packages get

Lastly, we need to extend Equatable

import 'package:equatable/equatable.dart';

class Person extends Equatable {
  const Person(this.name);

  final String name;

  @override
  List<Object> get props => [name];
}

When working with json:

import 'package:equatable/equatable.dart';

class Person extends Equatable {
  const Person(this.name);

  final String name;

  @override
  List<Object> get props => [name];

  factory Person.fromJson(Map<String, dynamic> json) {
    return Person(json['name']);
  }
}

We can now compare instances of Person just like before without the pain of having to write all of that boilerplate. Note: Equatable is designed to only work with immutable objects so all member variables must be final (This is not just a feature of Equatable - overriding a hashCode with a mutable value can break hash-based collections).

Equatable also supports const constructors:

import 'package:equatable/equatable.dart';

class Person extends Equatable {
  const Person(this.name);

  final String name;

  @override
  List<Object> get props => [name];
}

toString Implementation

Equatable can implement toString method including all the given props. If you want that behaviour for a specific Equatable object, just include the following:

@override
bool get stringify => true;

For instance:

import 'package:equatable/equatable.dart';

class Person extends Equatable {
  const Person(this.name);

  final String name;

  @override
  List<Object> get props => [name];

  @override
  bool get stringify => true;
}

For the name Bob, the output will be:

Person(Bob)

This flag by default is false and toString will return just the type:

Person

EquatableConfig

stringify can also be configured globally for all Equatable instances via EquatableConfig

EquatableConfig.stringify = true;

If stringify is overridden for a specific Equatable class, then the value of EquatableConfig.stringify is ignored. In other words, the local configuration always takes precedence over the global configuration.

Note: EquatableConfig.stringify defaults to true in debug mode and false in release mode.

Recap

Without Equatable

class Person {
  const Person(this.name);

  final String name;

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

  @override
  int get hashCode => name.hashCode;
}

With Equatable

import 'package:equatable/equatable.dart';

class Person extends Equatable {
  const Person(this.name);

  final String name;

  @override
  List<Object> get props => [name];
}

EquatableMixin

Sometimes it isn't possible to extend Equatable because your class already has a superclass. In this case, you can still get the benefits of Equatable by using the EquatableMixin.

Usage

Let's say we want to make an EquatableDateTime class, we can use EquatableMixin like so:

class EquatableDateTime extends DateTime with EquatableMixin {
  EquatableDateTime(
    int year, [
    int month = 1,
    int day = 1,
    int hour = 0,
    int minute = 0,
    int second = 0,
    int millisecond = 0,
    int microsecond = 0,
  ]) : super(year, month, day, hour, minute, second, millisecond, microsecond);

  @override
  List<Object> get props {
    return [year, month, day, hour, minute, second, millisecond, microsecond];
  }
}

Now if we want to create a subclass of EquatableDateTime, we can just override props.

class EquatableDateTimeSubclass extends EquatableDateTime {
  final int century;

  EquatableDateTimeSubclass(
    this.century,
    int year,[
    int month = 1,
    int day = 1,
    int hour = 0,
    int minute = 0,
    int second = 0,
    int millisecond = 0,
    int microsecond = 0,
  ]) : super(year, month, day, hour, minute, second, millisecond, microsecond);

  @override
  List<Object> get props => super.props..addAll([century]);
}

Performance

You might be wondering what the performance impact will be if you use Equatable.

Results (average over 10 runs)

Equality Comparison A == A

Class Runtime (μs)
Manual 0.193
Empty Equatable 0.191
Hydrated Equatable 0.190

Instantiation A()

Class Runtime (μs)
Manual 0.165
Empty Equatable 0.181
Hydrated Equatable 0.182

*Performance Tests run using: Dart VM version: 2.4.0

Maintainers

Changelog

2.0.0

  • BREAKING: opt into null safety
    • feat!: upgrade Dart SDK constraints to >=2.12.0-0 <3.0.0
  • BREAKING: stringify prints "null" for null properties instead of ""
  • feat: EquatableConfig.stringify defaults to true in debug mode.
  • fix: support legacy equality overrides with EquatableMixin
  • fix: iterable equality comparisons (#101)
  • fix: stringify instance with long properties (#94)
  • chore: update dependencies
    • collection: ^1.15.0
    • meta: ^1.3.0
  • docs: minor updates to README and example

2.0.0-nullsafety.4

  • feat: EquatableConfig.stringify defaults to true in debug mode.
  • fix: support legacy equality overrides with EquatableMixin

2.0.0-nullsafety.3

  • chore: update dependencies
    • collection: ^1.15.0
    • meta: ^1.3.0

2.0.0-nullsafety.2

  • fix: iterable equality comparisons (#101)
  • fix: stringify instance with long properties (#94)

2.0.0-nullsafety.1

  • BREAKING: stringify prints "null" for null properties instead of ""

2.0.0-nullsafety.0

  • BREAKING: opt into null safety
  • feat!: upgrade Dart SDK constraints to >=2.12.0-0 <3.0.0
  • docs: minor updates to README and example

1.2.6

  • fix: iterable equality comparisons (#101)
  • fix: stringify instance with long properties (#94)

1.2.5

  • docs: dartdoc improvements (#80)
  • docs: minor inline documentation improvements

1.2.4

  • fix: EquatableMixin stringify respects EquatableConfig.stringify (#81)

1.2.3

  • docs: inline, public documentation improvements (#78).
  • refactor: stricter analysis/lint rules

1.2.2

  • Documentation badge fixes and updates

1.2.1

  • Fix hashCode computation for Iterables (#74)
  • Minor documentation improvements

1.2.0

  • Added EquatableConfig for global stringify configuration (#69)

1.1.1

  • Updates to EquatableUtils documentation

1.1.0

  • Fix hashCode error when props is null (#45)
  • Added stringify feature (optional toString override) (#45)

1.0.3

  • Fix hashCode collisions for lists within props (#53)

1.0.2

  • Fix internal lint warnings

1.0.1

  • Fix hashCode collisions with Map properties (#43)

1.0.0

  • Update hashCode implementation to use Jenkins Hash (#39)
  • Documentation Updates

0.6.1

  • Minor documentation updates

0.6.0

  • The props getter override is required for both Equatable and EquatableMixin
  • Performance Improvements

0.5.1

  • Allow const constructors on Equatable class

0.5.0

  • Removed EquatableMixinBase (now covered by EquatableMixin).
  • Typed EquatableMixin from List<dynamic> to List<Object> to fix linter issues with implicit-dynamic: false.

0.4.0

Update toString to default to runtimeType (#27)

0.3.0

Enforce Immutability (#25)

0.2.6

Improved support for collection types (#19)

0.2.5

Improved support for Iterable, List, Map, and Set props (#17)

0.2.4

Additional Minor Documentation Updates

0.2.3

Documentation Updates

0.2.2

Bug Fixes:

  • Equatable instances that are equal now have the same hashCode (#8)

0.2.1

Update Dart support to >=2.0.0 <3.0.0

0.2.0

Add EquatableMixin and EquatableMixinBase

0.1.10

Enhancements to toString override

0.1.9

equatable has 0 dependencies

0.1.8

Support Iterable props

0.1.7

Added toString override

0.1.6

Documentation Updates

  • Performance Tests

0.1.5

Additional Performance Optimizations & Documentation Updates

0.1.4

Performance Optimizations

0.1.3

Bug Fixes

0.1.2

Additional Updates to Documentation.

  • Logo Added

0.1.1

Minor Updates to Documentation.

0.1.0

Initial Version of the library.

  • Includes the ability to extend Equatable and not have to override == and hashCode.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add 'equatable:2.0.0'
pubspec.yaml
name: equatable
description: A Dart package that helps to implement value based equality without needing to explicitly override == and hashCode.
version: 2.0.0
repository: https://github.com/felangel/equatable
issue_tracker: https://github.com/felangel/equatable/issues
homepage: https://github.com/felangel/equatable
documentation: https://github.com/felangel/equatable

environment:
  sdk: ">=2.12.0-0 <3.0.0"

dependencies:
  collection: ^1.15.0
  meta: ^1.3.0

dev_dependencies:
  coverage: ^0.14.2
  test: ^1.16.0

Version history

VersionPublishedArchiveSHA-256
0.1.0Cached · Upstream SHA-256 matchedDownloadca67495c…472d7c
0.1.1Cached · Upstream SHA-256 matchedDownloaddc8bb0e1…68a1c4
0.1.2Cached · Upstream SHA-256 matchedDownload544ccbad…d9ec16
0.1.3Cached · Upstream SHA-256 matchedDownload26ab9f9e…085fcf
0.1.4Cached · Upstream SHA-256 matchedDownloadd378b5ba…615730
0.1.5Cached · Upstream SHA-256 matchedDownload3e268d04…4e4c6a
0.1.6Cached · Upstream SHA-256 matchedDownload034cd780…f1a860
0.1.7Cached · Upstream SHA-256 matchedDownload14cf3af5…e9ca5b
0.1.8Cached · Upstream SHA-256 matchedDownloadbfbb2d74…ce7772
0.1.9Cached · Upstream SHA-256 matchedDownload3ddd6fd4…22383c
0.1.10Cached · Upstream SHA-256 matchedDownloadb15487c8…b1ad27
0.2.0Cached · Upstream SHA-256 matchedDownload3d088bcd…1292d0
0.2.1Cached · Upstream SHA-256 matchedDownload2f404fd7…363918
0.2.2Cached · Upstream SHA-256 matchedDownload36981922…32a0d8
0.2.3Cached · Upstream SHA-256 matchedDownload3e7f49ce…6095b2
0.2.4Cached · Upstream SHA-256 matchedDownloadbc5fbad0…5555d5
0.2.5Cached · Upstream SHA-256 matchedDownload0a1d7e46…ba66cb
0.2.6Cached · Upstream SHA-256 matchedDownload25b1afe7…163e60
0.3.0Cached · Upstream SHA-256 matchedDownload0f8be44d…3f64b9
0.4.0Cached · Upstream SHA-256 matchedDownloade96061e0…aa27fb
0.5.0Cached · Upstream SHA-256 matchedDownload5eda445d…926448
0.5.1Cached · Upstream SHA-256 matchedDownloadf109066e…2a8dc8
0.6.0Cached · Upstream SHA-256 matchedDownload6a0e2c6b…01d4e9
0.6.1Cached · Upstream SHA-256 matchedDownload4c12553f…d498e1
1.0.0Cached · Upstream SHA-256 matchedDownloade9c2e658…0a21c0
1.0.1Cached · Upstream SHA-256 matchedDownload426f31d6…99c461
1.0.2Cached · Upstream SHA-256 matchedDownload622a9c05…c4299e
1.0.3Cached · Upstream SHA-256 matchedDownload5b5eaa48…209b6f
1.1.0Cached · Upstream SHA-256 matchedDownload6df0c8b9…b5f927
1.1.1Cached · Upstream SHA-256 matchedDownloadcab4ec7b…0e2a36
1.2.0Cached · Upstream SHA-256 matchedDownloadcc5e9c6e…d5ca31
1.2.1Cached · Upstream SHA-256 matchedDownload121ae72b…03a29d
1.2.2Cached · Upstream SHA-256 matchedDownloadddb07d0f…ac2803
1.2.3Cached · Upstream SHA-256 matchedDownload0ea81210…123e84
1.2.4Cached · Upstream SHA-256 matchedDownloade43d1f19…819c35
1.2.5Cached · Upstream SHA-256 matchedDownload6d80ee2e…5513fc
1.2.6Cached · Upstream SHA-256 matchedDownload8007a033…618a03
2.0.0-nullsafety.0Cached · Upstream SHA-256 matchedDownload8781f93b…3e8ba4
2.0.0-nullsafety.1Cached · Upstream SHA-256 matchedDownloadd6c95425…80675d
2.0.0-nullsafety.2Cached · Upstream SHA-256 matchedDownload538d7f4b…3f2adc
2.0.0-nullsafety.3Cached · Upstream SHA-256 matchedDownload71fde743…d892ee
2.0.0-nullsafety.4Cached · Upstream SHA-256 matchedDownloadd8033f6b…eaee33
2.0.0Cached · Upstream SHA-256 matchedDownload8867afc0…ec97a4
2.0.1Cached · Upstream SHA-256 matchedDownload14263956…074384
2.0.2Cached · Upstream SHA-256 matchedDownloadda851e4d…9d81cb
2.0.3Cached · Upstream SHA-256 matchedDownloadc6094fd1…add375
2.0.4Cached · Upstream SHA-256 matchedDownload42d13ad5…96feb7
2.0.5Cached · Upstream SHA-256 matchedDownloadc2b87cb7…5e3ca2
2.0.6Cached · Upstream SHA-256 matchedDownloadb35578f6…3fa000
2.0.7Cached · Upstream SHA-256 matchedDownload567c64b3…04aef7
2.0.8Cached · Upstream SHA-256 matchedDownload3e014150…1a3e2b
2.1.0-dev.0Cached · Upstream SHA-256 matchedDownloadf8b1ad1d…c42dc0
2.1.0Cached · Upstream SHA-256 matchedDownload3bce007a…c5eaa2
3.0.0-dev.0Cached · Upstream SHA-256 matchedDownload4f531ad0…83980a
3.0.0-dev.1Cached · Upstream SHA-256 matchedDownload0b8e50fa…653198
3.0.0Cached · Upstream SHA-256 matchedDownloadea36e500…be1702

Dependencies

collection "^1.15.0"

meta "^1.3.0"