Operators
Arithmetic, comparison, logical, and null-aware operators — including the ones that don't exist in most other languages.
2 min de lecture
Most of Dart's operators will feel immediately familiar if you've used any C-family language. A few, though, are Dart-specific and solve real everyday problems — those are the ones worth slowing down for.
The familiar ones
int a = 10, b = 3;
print(a + b); // 13 addition
print(a - b); // 7 subtraction
print(a * b); // 30 multiplication
print(a / b); // 3.3333333333333335 division — always returns a double
print(a ~/ b); // 3 integer (truncating) division
print(a % b); // 1 remainderThe one to notice is ~/ — Dart splits "division" into two distinct operators because / always produces a double, even for int operands. If you want a whole-number result from dividing two integers, ~/ is how you ask for it, rather than dividing and separately rounding.
Comparison and logical operators
print(5 > 3); // true
print(5 == 5.0); // true — compares value, not type
print(5 != 4); // true
bool isAdmin = true;
bool isOwner = false;
print(isAdmin && isOwner); // false — both must be true
print(isAdmin || isOwner); // true — at least one is true
print(!isAdmin); // false — negationNull-aware operators
These are the operators that make working with nullable values (String?, int?, and so on) pleasant instead of a chain of manual if checks.
String? nickname;
// ?? — "use this value, or a fallback if it's null"
String displayName = nickname ?? 'Anonymous';
print(displayName); // Anonymous
// ??= — assign only if the variable is currently null
nickname ??= 'Guest';
print(nickname); // Guest
// ?. — call a method or read a property only if the receiver isn't null
int? length = nickname?.length;
print(length); // 5Without ?., reading nickname.length on a possibly-null variable wouldn't even compile — Dart is protecting you from a null-reference crash before the program runs, rather than after. ?. says "do this if there's a value, otherwise the whole expression is just null," which chains naturally with ?? when you need a guaranteed non-null result:
int safeLength = nickname?.length ?? 0;The cascade operator
One more Dart-specific operator worth an early mention is the cascade, .., which lets you perform a sequence of operations on the same object without repeating its name:
final buffer = StringBuffer()
..write('Hello, ')
..write('Dart')
..write('!');
print(buffer.toString()); // Hello, Dart!Each ..write(...) call returns the same buffer instead of void-ing it away, which is why they can be chained. You'll see cascades again once we get to building up objects with several properties at once.