equatable 2.0.7 copy "equatable: ^2.0.7" to clipboard
equatable: ^2.0.7 copied to clipboard

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

example/main.dart

// ignore_for_file: avoid_print

import 'package:equatable/equatable.dart';

class Credentials extends Equatable {
  const Credentials({required this.username, required this.password});

  final String username;
  final String password;

  @override
  List<Object> get props => [username, password];

  @override
  bool get stringify => false;
}

class EquatableDateTime extends DateTime with EquatableMixin {
  EquatableDateTime(
    super.year, [
    super.month,
    super.day,
    super.hour,
    super.minute,
    super.second,
    super.millisecond,
    super.microsecond,
  ]);

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

  @override
  bool get stringify => true;
}

void main() {
  // Extending Equatable
  const credentialsA = Credentials(username: 'Joe', password: 'password123');
  const credentialsB = Credentials(username: 'Bob', password: 'password!');
  const credentialsC = Credentials(username: 'Bob', password: 'password!');

  print(credentialsA == credentialsA); // true
  print(credentialsB == credentialsB); // true
  print(credentialsC == credentialsC); // true
  print(credentialsA == credentialsB); // false
  print(credentialsB == credentialsC); // true
  print(credentialsA); // Credentials

  // Equatable Mixin
  final dateTimeA = EquatableDateTime(2019);
  final dateTimeB = EquatableDateTime(2019, 2, 20, 19, 46);
  final dateTimeC = EquatableDateTime(2019, 2, 20, 19, 46);

  print(dateTimeA == dateTimeA); // true
  print(dateTimeB == dateTimeB); // true
  print(dateTimeC == dateTimeC); // true
  print(dateTimeA == dateTimeB); // false
  print(dateTimeB == dateTimeC); // true
  print(dateTimeA); // EquatableDateTime(2019, 1, 1, 0, 0, 0, 0, 0)
}
3.26k
likes
160
points
4.08M
downloads
screenshot

Publisher

verified publisherfluttercommunity.dev

Weekly Downloads

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

Repository (GitHub)
View/report issues
Contributing

Topics

#equality #equals

Documentation

Documentation
API reference

Funding

Consider supporting this project:

github.com

License

MIT (license)

Dependencies

collection, meta

More

Packages that depend on equatable