Dart Operators
Operators are the symbols that combine values: + adds, == compares, && joins conditions. Dart's operators look like those in most C-style languages, plus a few handy extras for working with null and for building objects.
Arithmetic operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ |
Add | 7 + 2 |
9 |
- |
Subtract | 7 - 2 |
5 |
* |
Multiply | 7 * 2 |
14 |
/ |
Divide (always double) |
7 / 2 |
3.5 |
~/ |
Integer divide | 7 ~/ 2 |
3 |
% |
Remainder | 7 % 2 |
1 |
-expr |
Negate | -7 |
-7 |
void main() {
var a = 17, b = 5;
print(a + b);
print(a - b);
print(a * b);
print(a / b);
print(a ~/ b);
print(a % b);
var count = 0;
count++; // increment
count += 10; // compound assignment
count *= 2;
print(count);
}22
12
85
3.4
3
2
22
++ and -- come in prefix (++x, change then use) and postfix (x++, use then change) forms. Compound assignments such as +=, -=, *=, ~/= and ??= are shortcuts for x = x op y.
Comparison and logical operators
| Operator | Meaning |
|---|---|
== / != |
Equal / not equal |
> < >= <= |
Greater / less than |
&& |
Logical AND (short-circuits) |
|| |
Logical OR (short-circuits) |
! |
Logical NOT |
void main() {
var age = 20;
var hasTicket = false;
print(age >= 18);
print(age >= 18 && hasTicket);
print(age >= 18 || hasTicket);
print(!hasTicket);
print('dart' == 'dart');
}true
false
true
true
true
In Dart, == compares values for strings and numbers. Classes can define their own meaning of ==. To check whether two references point to the same object, use identical(a, b).
The conditional (ternary) operator
condition ? valueIfTrue : valueIfFalse picks one of two values. It is extremely common inside Flutter build methods.
void main() {
var score = 72;
var result = score >= 50 ? 'pass' : 'fail';
print(result);
var itemCount = 1;
print('$itemCount ${itemCount == 1 ? 'item' : 'items'}');
}pass
1 item
Null-aware operators
These operators make code that deals with possibly-null values short and safe. You'll study them in depth in the Null Safety lesson.
| Operator | Name | Meaning |
|---|---|---|
a ?? b |
if-null | a if it isn't null, otherwise b |
a ??= b |
null-aware assignment | Assign b only if a is null |
a?.b |
null-aware access | null if a is null, otherwise a.b |
a! |
null assertion | Treat a as non-null (throws if it is null) |
void main() {
String? nickname;
print(nickname ?? 'Guest');
print(nickname?.length);
nickname ??= 'Bao';
print(nickname);
print(nickname.length);
}Guest
null
Bao
3
Type test operators
| Operator | Meaning |
|---|---|
is |
True if the object has the type |
is! |
True if it does not have the type |
as |
Cast to a type (throws if it's the wrong type) |
The cascade operator ..
The cascade .. lets you perform several operations on the same object without repeating its name. The whole expression evaluates to the original object.
void main() {
var buffer = StringBuffer()
..write('Hello')
..write(', ')
..write('cascade');
print(buffer.toString());
var numbers = <int>[]
..add(3)
..add(1)
..add(2)
..sort();
print(numbers);
}Hello, cascade
[1, 2, 3]
Tip:
?..is the null-aware cascade: it does nothing if the object is null.
Operator precedence
When operators are mixed, multiplication and division bind tighter than addition, comparisons come after arithmetic, and && binds tighter than ||. When in doubt, add parentheses — they cost nothing and make intent clear: (a + b) * c.
Test yourself
What is the type of the result of 10 / 2?
- int
- double
- num
- String
If name is null, what does name ?? 'Anon' return?
- null
- An error
- 'Anon'
- An empty string
What does the cascade expression list..add(1)..add(2) evaluate to?
- The value 2
- The list itself
- null
- true