felangel / equatable

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

Home Page:https://pub.dev/packages/equatable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Objects that are treated as equal have different hash codes in rare cases

kaboc opened this issue · comments

commented
class Foo extends Equatable {
  const Foo(this.map);

  final Map<String, int> map;

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

void main() {
  final foo1 = Foo({'a': 1, 'b': 2, 'c': 3});
  final foo2 = Foo({'b': 2, 'c': 3, 'a': 1});

  print(foo1 == foo2);   // true
  print(foo1.hashCode);  // 676937503
  print(foo2.hashCode);  // 623476217
}

For comparison, a similar class generated by Freezed from the following code does not have this issue.

@freezed
class Bar with _$Bar {
  const factory Bar(Map<String, int> map) = _Bar;
}

I'm not sure if it can cause some real trouble, but I thought I should at least report it.

Expected behavior

Matches the specification written in the document of the Object class.

Hash codes must be the same for objects that are equal to each other according to operator ==.

Version

Dart SDK version: 2.12.2 (stable) (Wed Mar 17 10:30:20 2021 +0100) on "windows_x64"