Operators in Dart
Learn arithmetic, comparison, logical, assignment, and null-aware operators in Dart with practical Docker-ready examples
Operators are the building blocks of expressions. Dart provides a rich set of operators familiar to anyone with a C-style language background, plus a handful of features that reflect its modern design: integer-specific division, sound null safety, and cascade notation for chained method calls.
Because Dart is statically and strongly typed with sound null safety, the compiler verifies operator usage at compile time. You cannot add a String to an int implicitly, and the analyzer will refuse to dereference a potentially-null value with .. This makes operators safer to use than in many dynamic languages, while still feeling concise thanks to type inference.
In this tutorial you will learn how Dart handles arithmetic (including the distinction between / and ~/), comparisons, logical operators with short-circuit evaluation, compound assignment, string concatenation and interpolation, null-aware operators (?., ??, ??=), and a few Dart-specific operators like cascades (..) and conditional expressions.
Arithmetic Operators
Dart supports the standard arithmetic operators, with one important distinction: / always returns a double, while ~/ performs integer (truncating) division.
Create a file named operators_arithmetic.dart:
| |
Notice how 17 / 5 produces 3.4 (a double), while 17 ~/ 5 produces 3 (an int). Dart’s static type system tracks this for you — the result type of / is always double, even when both operands are int.
Comparison and Logical Operators
Comparison operators return bool. Logical operators (&&, ||, !) short-circuit just like in C, Java, and JavaScript.
Create a file named operators_logic.dart:
| |
The is and is! operators are Dart-specific type tests. Combined with smart promotion, if (value is String) { ... } lets the analyzer treat value as a String inside the block without an explicit cast.
Assignment, Strings, and Conditional Expressions
Dart provides the usual compound assignment operators (+=, -=, *=, etc.) and uses + for string concatenation — though interpolation is more idiomatic. The ternary ?: and the if-null ?? operator make many if statements unnecessary.
Create a file named operators_assign.dart:
| |
The String * int operator repeats the string, a small convenience absent from many C-style languages. Compound operators evaluate the left-hand expression only once, which matters when it has side effects.
Null-Aware and Cascade Operators
Dart’s sound null safety introduces a family of operators for working with nullable values without sprinkling if (x != null) checks everywhere. The cascade operator (..) lets you chain calls on the same object — handy for builder-style configuration.
Create a file named operators_null.dart:
| |
The cascade returns the receiver rather than the result of each method, which is why we can chain ..write(...)..newline() without losing the Buffer reference. Combined with ?., ??, and ??=, these operators are some of the most distinctly “Dart” features you’ll use daily.
Running with Docker
| |
Expected Output
Running operators_arithmetic.dart:
a + b = 22
a - b = 12
a * b = 85
a / b = 3.4
a ~/ b = 3
a % b = 2
-x = -10
counter = 2
Running operators_logic.dart:
x == y: false
x != y: true
x < y: true
x > y: false
x <= y: true
x >= y: false
inRange: true
isEdge: false
negated: false
value is String: true
value is! int: true
Running operators_assign.dart:
total = 9
Hello, Dart!
Hello, Dart!
--------------------
grade: pass
Running operators_null.dart:
displayA: anonymous
displayB: Ada
length of maybeName: null
length of givenName: 3
title: Untitled
first
second
Key Concepts
- Two division operators:
/always returns adouble;~/truncates toint. Choose deliberately based on the type you need. - Short-circuit logical operators:
&&and||stop evaluating as soon as the result is determined — useful for guarding against null or expensive operations. - Type-test operators:
isandis!enable type promotion, letting the analyzer narrow a variable’s type inside a conditional block. - String interpolation beats
+:'$name has ${items.length} items'is idiomatic; reserve+for cases where interpolation is awkward. - Null-aware family:
?.(safe access),??(default value), and??=(assign-if-null) eliminate most explicit null checks under sound null safety. - Cascades chain on one object:
obj..a()..b()..c()evaluates toobj, not to the result ofc()— ideal for configuration and builders. - Compound assignments evaluate once:
list[i++] += 1incrementsia single time, which differs from rewriting it aslist[i++] = list[i++] + 1. - Operators are methods: Most arithmetic and comparison operators can be overloaded on your own classes by defining methods like
operator +(other).
Comments
Loading comments...
Leave a Comment