operator == method

  1. @override
bool operator == (
  1. Object other
)
override

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:

  • Total: It must return a boolean for all arguments. It should never throw or return null.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must be true.

The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

Implementation

@override
bool operator ==(Object other) {
  if (identical(this, other))
    return true;
  if (other is! Locale) {
    return false;
  }
  final String? countryCode = _countryCode;
  final String? otherCountryCode = other.countryCode;
  return other.languageCode == languageCode
      && other.scriptCode == scriptCode // scriptCode cannot be ''
      && (other.countryCode == countryCode // Treat '' as equal to null.
          || otherCountryCode != null && otherCountryCode.isEmpty && countryCode == null
          || countryCode != null && countryCode.isEmpty && other.countryCode == null);
}