Skip to content

Commit dafda50

Browse files
author
Jonah Williams
authored
[framework] remove hashcode override for Element (#96644)
1 parent 49c5871 commit dafda50

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

packages/flutter/lib/src/widgets/framework.dart

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,30 +3137,9 @@ abstract class Element extends DiagnosticableTree implements BuildContext {
31373137
/// in a build method if it is known that they will not change.
31383138
@nonVirtual
31393139
@override
3140-
// ignore: avoid_equals_and_hash_code_on_mutable_classes
3140+
// ignore: avoid_equals_and_hash_code_on_mutable_classes, hash_and_equals
31413141
bool operator ==(Object other) => identical(this, other);
31423142

3143-
// Custom implementation of hash code optimized for the ".of" pattern used
3144-
// with `InheritedWidgets`.
3145-
//
3146-
// `Element.dependOnInheritedWidgetOfExactType` relies heavily on hash-based
3147-
// `Set` look-ups, putting this getter on the performance critical path.
3148-
//
3149-
// The value is designed to fit within the SMI representation. This makes
3150-
// the cached value use less memory (one field and no extra heap objects) and
3151-
// cheap to compare (no indirection).
3152-
//
3153-
// See also:
3154-
//
3155-
// * https://dart.dev/articles/dart-vm/numeric-computation, which
3156-
// explains how numbers are represented in Dart.
3157-
@nonVirtual
3158-
@override
3159-
// ignore: avoid_equals_and_hash_code_on_mutable_classes
3160-
int get hashCode => _cachedHash;
3161-
final int _cachedHash = _nextHashCode = (_nextHashCode + 1) % 0xffffff;
3162-
static int _nextHashCode = 1;
3163-
31643143
/// Information set by parent to define where this child fits in its parent's
31653144
/// child list.
31663145
///

packages/flutter/test/widgets/framework_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,21 @@ The findRenderObject() method was called for the following element:
16561656
)),
16571657
);
16581658
});
1659+
1660+
testWidgets('Elements use the identity hashCode', (WidgetTester tester) async {
1661+
final StatefulElement statefulElement = StatefulElement(const _StatefulLeaf());
1662+
expect(statefulElement.hashCode, identityHashCode(statefulElement));
1663+
1664+
final StatelessElement statelessElement = StatelessElement(const Placeholder());
1665+
1666+
expect(statelessElement.hashCode, identityHashCode(statelessElement));
1667+
1668+
final InheritedElement inheritedElement = InheritedElement(
1669+
const Directionality(textDirection: TextDirection.ltr, child: Placeholder()),
1670+
);
1671+
1672+
expect(inheritedElement.hashCode, identityHashCode(inheritedElement));
1673+
});
16591674
}
16601675

16611676
class _WidgetWithNoVisitChildren extends StatelessWidget {

0 commit comments

Comments
 (0)