Solved: what is operator == method in flutter?

Question

Asked by Pannam on December 01, 2021 (source).

As per the documentation https://api.flutter.dev/flutter/painting/BoxShadow/operator_equals.html has its implementation as follows

@override
bool operator ==(Object other) {
  if (identical(this, other))
    return true;
  if (other.runtimeType != runtimeType)
    return false;
  return other is BoxShadow
      && other.color == color
      && other.offset == offset
      && other.blurRadius == blurRadius
      && other.spreadRadius == spreadRadius;
}

and the hashcode property as follows

@override
int get hashCode => hashValues(color, offset, blurRadius, spreadRadius);

What does this actually do? and where is it useful? What is the purpose of opeartor, runtimeType, and hasCode etc in the code? Would be great if you could provide some examples too in simpler terms.

Answer

Question answered by JaffaKetchup (source).

The keyword operator is used in Dart Extension Methods. For more info about extension methods, and their uses, see here: https://youtu.be/D3j0OSfT9ZI.

In Dart, objects (by default) won't equal each other, even if the properties are the same: because Dart does not automatically perform deep equality (the process of checking each property of an object), and the instances are separate.

The operator is, in this case, overriding the default equality checks, and performing a deep equality check, and checking that the runtime type (for example, String or BoxShadow or Widget) is the same. Any code can be run on equality, but it's bad practise to run anything other than more equality checks. The hash code overriding is necessary when overriding the equality operator, as Dart uses this internally to perform other equality operations - the hash code basically converts an object and it's properties into a String, that will be the same with a similar object.

DART FLUTTER
SHARE: