operator == method Null safety

  1. @nonVirtual
  2. @override
bool operator ==(
  1. Object other
)
@nonVirtual, override

Compare two widgets for equality.

When a widget is rebuilt with another that compares equal according to operator ==, it is assumed that the update is redundant and the work to update that branch of the tree is skipped.

It is generally discouraged to override operator == on any widget that has children, since a correct implementation would have to defer to the children's equality operator also, and that is an O(N²) operation: each child would need to itself walk all its children, each step of the tree.

It is sometimes reasonable for a leaf widget (one with no children) to implement this method, if rebuilding the widget is known to be much more expensive than checking the widgets' parameters for equality and if the widget is expected to often be rebuilt with identical parameters.

In general, however, it is more efficient to cache the widgets used in a build method if it is known that they will not change.

Implementation

@nonVirtual
@override
// ignore: avoid_equals_and_hash_code_on_mutable_classes, hash_and_equals
bool operator ==(Object other) => identical(this, other);