---
title: "Applying Arithmetic, Relational, Logical, and Null-Aware Operators in Dart"
date: "2026-12-03T09:57:00+07:00"
description: "The full Dart operator precedence table, arithmetic and bitwise families, logical and null-aware operators, plus the gotchas that make parentheses mandatory."
author: "AI Assistant"
image: "https://blog.redlinesoft.net/images/post/dart-operators-arithmetic-logical-null-aware.jpg"
alt: "A layered chart of Dart operator precedence from unary postfix down to assignment and spread"
tags: ['dart', 'dart-language', 'operators', 'precedence', 'null-safety']
---

Knowing every operator is easy; knowing which one binds tighter is what separates code that works from code that mysteriously does something else. This is the complete Dart operator set ordered from highest to lowest precedence, followed by each operator family and the traps the official documentation warns about.

## The full precedence table

Rows higher in the table bind more tightly, so they are evaluated first. The documentation calls the table an approximation of the real grammar: "None" associativity means those operators cannot be chained without parentheses, while "Left" and "Right" tell you which side groups first when precedence ties — treat it as a reading aid rather than a specification.

| Description | Operator | Associativity |
|---|---|---|
| unary postfix | `expr++`, `expr--`, `()`, `[]`, `?[]`, `.`, `?.`, `!` | None |
| unary prefix | `-expr`, `!expr`, `~expr`, `++expr`, `--expr`, `await expr` | None |
| multiplicative | `*`, `/`, `%`, `~/` | Left |
| additive | `+`, `-` | Left |
| shift | `<<`, `>>`, `>>>` | Left |
| bitwise AND | `&` | Left |
| bitwise XOR | `^` | Left |
| bitwise OR | `\|` | Left |
| relational and type test | `>=`, `>`, `<=`, `<`, `as`, `is`, `is!` | None |
| equality | `==`, `!=` | None |
| logical AND | `&&` | Left |
| logical OR | `\|\|` | Left |
| if-null | `??` | Left |
| conditional | `expr1 ? expr2 : expr3` | Right |
| cascade | `..`, `?..` | Left |
| assignment | `=`, `*=`, `/=`, `+=`, `-=`, `&=`, `^=`, etc. | Right |
| spread | `...`, `...?` | None |

## Arithmetic operators and increment

`/` always produces a `double`, `~/` truncates toward zero to produce an `int`, and `%` is the Euclidean modulo, whose result always lands in `[0, |other|)` — so `-5 % 3` is `1` rather than `-1`. The prefix form `++count` increments first and then yields the value, while the postfix form `count++` yields the old value and increments afterward:

```dart
void main() {
  print(5 / 2); // 2.5 — division always yields a double
  print(5 ~/ 2); // 2 — integer division
  print(5 % 2); // 1 — remainder
  print(-5 % 3); // 1 — Euclidean modulo, always non-negative
  var count = 0;
  final before = count++;
  final after = ++count;
  print('before=$before, after=$after'); // before=0, after=2
}
```

Prefer the postfix form as a standalone statement and the prefix form when the result of the expression itself matters, since both mutate the variable identically.

## Relational, equality, and type tests

Relational operators compare values, while `is`, `is!`, and `as` test and cast types at runtime. Inside an `if (value is String)` block, Dart promotes `value` to `String`, so `value.length` compiles without a cast; the `as` operator instead throws if the object is not of that type, which is why the documentation says to cast only when you are sure. What `==` actually means for objects — and how it differs from `identical()` — is covered in `dart-equality-identical-hashcode.md`.

```dart
void main() {
  final age = 20;
  print(age >= 18 && age < 65); // true
  Object value = '42';
  if (value is String) {
    print(value.length); // promoted to String inside the branch
  }
  final number = int.parse(value as String);
  print(number + 8); // 50
}
```

## Logical operators

Both `&&` and `||` short-circuit: the right operand never runs when the left one already decides the answer. Short-circuiting is not just a performance detail — it is the standard way to guard against nulls and errors, as in `x != null && x.isNotEmpty`. Because `&&` binds tighter than `||`, the parentheses in the documentation's example `!done && (col == 0 || col == 3)` are exactly what group the OR first.

```dart
void main() {
  var calls = 0;
  bool sideEffect() {
    calls++;
    return true;
  }
  print(false && sideEffect()); // false
  print(calls); // 0 — the right side never ran
  print(true || sideEffect()); // true
  print(calls); // still 0
}
```

## Bitwise and shift operators

`&`, `|`, `^`, `~` and the three shift operators work on the bits of `int` values, which is how flags and bit masks are manipulated. The `>>>` operator requires a language version of at least 2.14, and the documentation notes that bitwise behavior with large or negative operands can differ between native and web platforms because the web masks operands to 32 bits:

```dart
void main() {
  const value = 0x22;
  const bitmask = 0x0f;
  print(value & bitmask); // 2 — AND
  print(value | bitmask); // 47 — OR
  print(value ^ bitmask); // 45 — XOR
  print(~bitmask & value); // 32 — AND NOT
  print(value << 4); // 544 — shift left
  print(value >> 4); // 2 — arithmetic shift right
  print((-value) >>> 4 > 0); // true — unsigned shift ignores the sign
}
```

## Null-aware operators, ??=, and the conditional expression

Compound assignment folds an operation into the assignment — `a op= b` means `a = a op b`, except that `a` is evaluated only once — and `??=` assigns only when the target is null. For producing a value, the conditional `?:` handles booleans and the if-null `??` handles null checks; both replace small `if`/`else` blocks, while anything needing statements belongs in the real branching forms from `dart-if-else-switch-statements.md`:

```dart
String playerName(String? name) => name ?? 'Guest';

void main() {
  int? retries;
  retries ??= 3;
  retries ??= 10;
  print(retries); // 3 — already set, so the second write is skipped
  print(playerName(null)); // Guest
  final isPublic = true;
  print(isPublic ? 'public' : 'private'); // public
}
```

Pair `?.` with `!` carefully: `?.` stops the chain when the receiver is null, while `!` asserts it is not and throws otherwise.

## Cascades, spread, and precedence gotchas

Cascades (`..`) sequence operations on the same object, saving temporary variables, and `?..` does the same while short-circuiting on a null receiver. Strictly speaking, cascades are syntax rather than operators, and spread operators are part of collection literals with no precedence at all — which is why `[...a + b]` is valid. One classic error is chaining off a `void` result, so `sb.write('foo')..write('bar')` fails to compile because there is nothing to cascade from. The full tour of the notation lives in `dart-cascade-notation-smalltalk-fluent-api.md`.

```dart
void main() {
  final buffer = StringBuffer()
    ..write('Dart')
    ..write(' ')
    ..write('operators');
  print(buffer.toString()); // Dart operators
}
```

When mixing operator families, parentheses earn their keep: because `%` binds tighter than `==`, which binds tighter than `&&`, the lines `if ((n % i == 0) && (d % i == 0))` and `if (n % i == 0 && d % i == 0)` execute identically, and only the first documents its intent. The other rule comes with a warning in the docs — for any operator that takes two operands, the leftmost operand determines which method runs. Given a `Vector` and a `Point`, `aVector + aPoint` calls `Vector`'s `operator +`, and the `Point`'s own addition logic is never consulted.

## Takeaways

- Read the table top to bottom: unary binds tightest, then arithmetic, shift and bitwise, relational, equality, logical, null-aware, conditional, cascade, and finally assignment.
- `/` always returns a `double`; use `~/` for integer division and `%` for the Euclidean modulo, whose result is always non-negative.
- `is` promotes the tested value inside the branch; `as` throws instead of promoting, so cast only when you are sure.
- The leftmost operand picks the overloaded operator method, `??=` writes only when null, and `&&`/`||` short-circuit — parentheses are the cheapest defense against precedence surprises.

**Takeaway:** When in doubt about how a Dart expression groups, add parentheses — the precedence table is an approximation of the grammar, but explicit grouping is never wrong and always reads faster in review.

## References

- https://dart.dev/language/operators
- https://dart.dev/language/built-in-types
- https://dart.dev/language/branches
