TExcelTAD
Home › Dart › Dart Operators

Dart Operators

Operators are symbols that compute a value from one or more operands. Dart has the usual arithmetic and comparison operators plus a few special ones — null-aware operators, cascades and type tests — that make code shorter and safer.

Arithmetic and assignment

dart · try it yourself
void main() {
  var a = 17, b = 5;
  print('${a + b} ${a - b} ${a * b} ${a / b} ${a ~/ b} ${a % b}');

  var x = 10;
  x += 5;   // 15
  x -= 3;   // 12
  x *= 2;   // 24
  x ~/= 5;  // 4
  print(x);

  var i = 1;
  print(i++); // prints 1, then i becomes 2
  print(++i); // i becomes 3, then prints 3
  print(-i);
}
22 12 85 3.4 3 2
4
1
3
-3

Comparison and equality

Operator Meaning
==, != Equal / not equal (calls the == method)
>, <, >=, <= Ordering
identical(a, b) Same object in memory (a function, not an operator)

== can be overridden by classes. Two distinct List objects with the same contents are not ==, because List uses identity equality:

dart · try it yourself
void main() {
  print(3 == 3.0);
  print('ab' == 'a' + 'b');
  print([1, 2] == [1, 2]);
  print(identical(const [1], const [1]));
  print((1, 'x') == (1, 'x')); // records compare by value
}
true
true
false
true
true

Logical and conditional

  • !, &&, || — see Booleans.
  • cond ? a : b — the conditional (ternary) operator.
  • a ?? b — a if it isn't null, otherwise b.
dart · try it yourself
void main() {
  var stock = 0;
  String? nickname;

  print(stock > 0 ? 'In stock' : 'Sold out');
  print(nickname ?? 'anonymous');

  nickname ??= 'guest'; // assign only if null
  nickname ??= 'ignored';
  print(nickname);
}
Sold out
anonymous
guest

Null-aware operators

Operator Example Meaning
?. user?.email Member access only when not null
?? a ?? b Default value
??= a ??= b Assign if null
! a! Assert non-null (throws if null)
?.. obj?..a()..b() Null-aware cascade
?[] list?[0] Null-aware index
...? [...?maybeList] Null-aware spread

They are covered in depth in Null Safety.

Cascade notation ..

A cascade performs several operations on the same object and evaluates to that object — no temporary variable needed.

dart · try it yourself
class Order {
  final List<String> lines = [];
  String customer = '';
  bool express = false;

  void add(String item) => lines.add(item);
  @override
  String toString() => '$customer: $lines${express ? ' (express)' : ''}';
}

void main() {
  final order = Order()
    ..customer = 'Bao'
    ..add('Coffee')
    ..add('Croissant')
    ..express = true;
  print(order);

  final sorted = [5, 3, 9]..sort();
  print(sorted);
}
Bao: [Coffee, Croissant] (express)
[3, 5, 9]

Note [5, 3, 9]..sort() returns the list, whereas [5, 3, 9].sort() returns void.

Bitwise and shift operators

Operator Meaning 6 ? 3
& AND 2
| OR 7
^ XOR 5
~ Complement (unary) —
<<, >> Shift left / right 48, 0
>>> Unsigned shift right 0
dart · try it yourself
void main() {
  const read = 1, write = 2, exec = 4;
  var perms = read | write;
  print(perms);
  print(perms & write != 0);
  print(perms & exec != 0);
  print(1 << 10);
}
3
true
false
1024

Precedence (high to low, abbreviated)

Group Operators
Postfix expr++, (), [], ., ?., !
Prefix -expr, !expr, ~expr, ++expr, await
Multiplicative *, /, ~/, %
Additive +, -
Shift / bitwise <<, >>, &, ^, |
Relational / type <, >=, as, is, is!
Equality ==, !=
Logical &&, then ||
If-null ??
Conditional ? :
Cascade .., ?..
Assignment =, +=, ??=, …

Tip: When in doubt, add parentheses. (a ?? 0) + 1 is clearer than relying on precedence rules.

Test yourself

What does [3, 1, 2]..sort() evaluate to?

  1. void
  2. The sorted list [1, 2, 3]
  3. null
  4. A new unsorted list

After int? n; n ??= 5; n ??= 9; what is n?

  1. null
  2. 5
  3. 9
  4. 14

What is 17 ~/ 5?

  1. 3.4
  2. 3
  3. 2
  4. 4