fix: hashCode computation for Iterables (#76)
diff --git a/lib/src/equatable_utils.dart b/lib/src/equatable_utils.dart
index a5e0d52..a0b7168 100644
--- a/lib/src/equatable_utils.dart
+++ b/lib/src/equatable_utils.dart
@@ -37,9 +37,14 @@
});
return hash;
}
- final objectHashCode =
- object is Iterable ? mapPropsToHashCode(object) : object.hashCode;
- hash = 0x1fffffff & (hash + objectHashCode);
+ if (object is Iterable) {
+ for (final value in object) {
+ hash = hash ^ _combine(hash, value);
+ }
+ return hash ^ object.length;
+ }
+
+ hash = 0x1fffffff & (hash + object.hashCode);
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}
diff --git a/test/equatable_test.dart b/test/equatable_test.dart
index 0b874b8..511fc5d 100644
--- a/test/equatable_test.dart
+++ b/test/equatable_test.dart
@@ -131,6 +131,7 @@
void main() {
EquatableConfig.stringify = false;
+
group('Empty Equatable', () {
test('should correct toString', () {
final instance = EmptyEquatable();
@@ -611,6 +612,12 @@
group('Collection Equatable', () {
group('Iterable Equatable', () {
+ test('list of zeros same hashcode check', () {
+ final s0 = SimpleEquatable([0, 0]);
+ final s1 = SimpleEquatable([0, 0, 0]);
+ expect(s0.hashCode != s1.hashCode, true);
+ });
+
test('should return when values are same', () {
final instanceA = SimpleEquatable<Iterable>(["A", "B"]);
final instanceB = SimpleEquatable<Iterable>(["A", "B"]);