Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The != (inequality) operator is an equality operator that evaluates to true if its two operands are not equivalent, and false otherwise. In Dart, != is strictly syntactic sugar for the logical negation of the equality operator (==).
expression1 != expression2

Underlying Mechanics

Unlike the equality operator (==), the != operator cannot be explicitly overridden in a Dart class. When the Dart compiler encounters the != operator, it automatically translates the expression into the negated invocation of the underlying == evaluation. The expression a != b is evaluated exactly as:
!(a == b)
Because of this translation, the behavior of != is entirely dictated by the implementation of the == method on the left-hand operand’s class, combined with Dart’s built-in null handling.

Evaluation Sequence

When evaluating a != b, the Dart runtime resolves the underlying a == b expression and negates the result using the following strict sequence:
  1. Null Check: The runtime first checks both operands for null.
    • If both a and b are null, a == b yields true (causing != to return false).
    • If exactly one operand is null (e.g., a is not null but b is null), a == b yields false (causing != to return true).
  2. Method Invocation: If and only if neither a nor b is null, the runtime invokes the == method on the left-hand operand: a.==(b). The != operator then returns the logical NOT (!) of the boolean returned by that method.
This sequence ensures that a.==(null) is never invoked. This is a fundamental requirement of Dart’s sound null safety, as the == operator’s parameter type is a non-nullable Object. The Dart runtime does not automatically perform an identity check (identical(a, b)) during this sequence. If an identity check is desired for performance optimization, it must be explicitly implemented by the developer inside the overridden == method.

Syntax Visualization

The following code demonstrates how defining the == operator implicitly defines the behavior of the != operator, and illustrates the null-check short-circuiting behavior:
class Entity {
  final int id;

  Entity(this.id);

  // Overriding == automatically provides the logic for !=
  @override
  bool operator ==(Object other) {
    // The identity check is a developer-implemented optimization, 
    // not an automatic runtime step.
    if (identical(this, other)) return true;
    
    return other is Entity && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}

void main() {
  Entity entityA = Entity(100);
  Entity entityB = Entity(200);
  Entity entityC = Entity(100);

  // Evaluates as: !(entityA == entityB)
  bool isDifferent = entityA != entityB; // true
  
  // Evaluates as: !(entityA == entityC)
  bool isDifferentValue = entityA != entityC; // false
  
  // Evaluates null checks before method invocation.
  // entityA.== is never invoked because the right operand is null.
  bool isNotNull = entityA != null; // true
}
Master Dart with Deep Grasping Methodology!Learn More