> ## 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.

# Dart Operator Method

An operator method in Dart is a specialized instance method that redefines the behavior of standard lexical operators for user-defined classes. By declaring a method with the `operator` keyword followed by the target operator symbol, developers implement custom operator overloading, allowing class instances to be manipulated using standard mathematical, logical, or indexing syntax.

## Syntax

The declaration of an operator method strictly follows this signature pattern:

```dart theme={"dark"}
ReturnType operator <symbol>(ParameterType parameter) {
  // Implementation
}
```

## Arity and Parameter Rules

The number of parameters an operator method accepts is strictly dictated by the arity of the operator being overridden. The left-hand operand is always the receiving instance (`this`).

* **Unary Operators:** Accept exactly **zero** parameters. The operation is performed entirely on `this`.

```dart theme={"dark"}
ReturnType operator ~() => ...
ReturnType operator -() => ... // Unary minus (negation)
```

* **Binary Operators:** Accept exactly **one** parameter, representing the right-hand operand.

```dart theme={"dark"}
ReturnType operator +(ParameterType other) => ...
ReturnType operator -(ParameterType other) => ... // Binary subtraction
```

*Note on the minus operator:* Dart distinguishes between the unary minus (negation) and binary minus (subtraction) entirely by the method's arity. Declaring `operator -()` with zero parameters overloads the unary minus, while declaring `operator -(ParameterType other)` with one parameter overloads binary subtraction.

* **Index Operators:**
  * The read index operator `[]` accepts exactly **one** parameter (the index).
  * The write index operator `[]=` accepts exactly **two** parameters (the index and the value to assign).

```dart theme={"dark"}
ReturnType operator [](IndexType index) => ...
void operator []=(IndexType index, ValueType value) { ... }
```

## Overridable Operators

Dart restricts operator overloading to a specific subset of lexical tokens. Only the following operators can be defined as methods:

* **Arithmetic:** `+`, `-` (both unary and binary), `*`, `/`, `~/` (integer division), `%`
* **Relational:** `==`, `<`, `>`, `<=`, `>=`
* **Bitwise:** `~`, `&`, `|`, `^`, `<<`, `>>`, `>>>`
* **Index:** `[]`, `[]=`

## Non-Overridable Operators

The Dart compiler enforces strict semantic consistency for certain operations, making the following operators ineligible for overloading:

* **Logical:** `&&`, `||`, `!`
* **Assignment:** `=`, `??=`
* **Compound Assignment:** `+=`, `-=`, `*=`, etc. (These are implicitly resolved by the compiler using the base operator, e.g., `a += b` evaluates to `a = a + b`).
* **Inequality:** `!=` (The compiler automatically derives this by evaluating `!(a == b)`).
* **Type Test/Cast:** `is`, `is!`, `as`
* **Control Flow / Null-Aware:** `?:`, `??`, `?.`, `.`

## Architectural Constraints

1. **The `==` Contract:** When overriding the equality operator (`==`), the parameter type must be exactly `Object`. You do not need to use `Object?` because Dart's compiler automatically short-circuits null comparisons (e.g., `a == null`) before the instance method is ever invoked, guaranteeing that `other` will be non-null. Furthermore, overriding `==` mandates a corresponding override of the `hashCode` getter to maintain the integrity of hash-based collections (like `Set` and `Map`).

```dart theme={"dark"}
@override
bool operator ==(Object other) {
  if (identical(this, other)) return true;
  return other is MyClass && other.internalValue == internalValue;
}

@override
int get hashCode => internalValue.hashCode;
```

2. **Instance Bound:** Operator methods must be instance methods. They cannot be declared as `static`.
3. **Extension Methods:** Operators can be added to existing types (including built-in types) using Dart's `extension` feature, provided the underlying type does not already define that specific operator method.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
