Decision Logic in Dart: if/else, if-case, and switch Statements
From plain if/else through Dart 3 if-case, switch statements, switch expressions, guards, and exhaustiveness checking over enums and sealed classes.
Published on • December 3, 2026
AI Assistant

Branching is where a program stops being a straight line, and Dart 3 gave you more tools here than most languages: plain if, pattern-matching if-case, fallthrough-free switch statements, value-producing switch expressions, guards, and compiler-checked exhaustiveness.
if/else with a strict boolean condition
Dart requires the condition after if to be an expression that evaluates to a bool. There is no truthiness — if (name) or if (count) is a compile error — so you check for what you mean instead: isEmpty for empty strings, == 0 for zero, == null for null, and isNaN for not-a-number. Every condition then reads as the precise question being asked, and the else if chain keeps mutually exclusive branches in one readable block:
void main() {
final weather = 'rain';
if (weather == 'rain') {
print('Bring a raincoat');
} else if (weather == 'snow') {
print('Wear a jacket');
} else {
print('Put the top down');
}
final name = '';
if (name.isEmpty) print('no name supplied'); // explicit — no truthiness
}
if-case: one pattern, one branch
Dart 3 added case clauses to if statements, letting you match a single value against a single pattern and destructure it in the same breath. When the list pattern [int x, int y] matches, the branch runs with x and y bound; otherwise control moves to else. The optional when clause adds a guard on top of the match. Use if-case when you are testing one value against one pattern — for several competing cases, switch is the better fit:
void main() {
final pair = [2, 5];
if (pair case [int x, int y]) {
print('coordinate $x,$y'); // coordinate 2,5
} else {
throw FormatException('Invalid coordinates.');
}
if (pair case [int x, int y] when x < y) {
print('$x is less than $y'); // 2 is less than 5
}
}
switch statements without fallthrough surprises
A switch statement evaluates a value against a series of case patterns, and its rules are the opposite of C-style switches: non-empty cases jump to the end of the switch when they finish, so they need no break, while empty cases fall through to share the next case’s body. continue someLabel; handles non-sequential fall-through, and default or the wildcard _ clause catches everything else — labels are covered in dart-break-continue-labels.md:
void main() {
const command = 'OPEN';
switch (command) {
case 'DENIED':
case 'OPEN': // empty case falls through and shares the body
print('executeOpen');
case 'CLOSED':
print('executeClosed');
default:
print('unknown command');
}
}
switch expressions produce a value
Where a switch statement does something, a switch expression returns something, which makes it ideal for assignments, arguments, and return statements. The syntax differs in five ways: cases drop the case keyword, each body is a single expression after =>, every case must have a body so there is no implicit fallthrough, cases are separated by commas, and the default arm can only be written as _:
String classify(int charCode) => switch (charCode) {
47 || 42 || 43 || 45 => 'operator', // slash, star, plus, minus
44 || 59 => 'punctuation', // comma, semicolon
>= 48 && <= 57 => 'number', // digit 0 through 9
_ => 'unknown',
};
void main() {
print(classify(43)); // operator
print(classify(55)); // number
}
Guards: refining a match with when
A guard clause follows any pattern with the when keyword and evaluates an arbitrary boolean expression after the pattern matches. The distinction that matters: when a guard returns false, execution proceeds to the next case. Putting the same check in an if inside the case body would instead fall out of the entire switch. Guards work identically in switch statements, switch expressions, and if-case:
void main() {
final pair = (3, 9);
switch (pair) {
case (int a, int b) when a < b:
print('second element greater: $b > $a'); // matches here
case (int a, int b):
print('both equal or descending');
}
}
Exhaustiveness checking with enums and sealed classes
Dart reports a compile-time error if a value could reach a switch but match no case. A default or _ arm makes any switch exhaustive, but enums and sealed classes are checked even without one, because the compiler knows every possible subtype: add a Triangle implements Shape and the expression below stops compiling until you handle it. The same check catches a switch on bool? that handles true and false but forgets null:
import 'dart:math';
sealed class Shape {}
class Square implements Shape { final double length; Square(this.length); }
class Circle implements Shape { final double radius; Circle(this.radius); }
double calculateArea(Shape shape) => switch (shape) {
Square(length: var l) => l * l,
Circle(radius: var r) => pi * r * r,
};
void main() {
print(calculateArea(Square(3))); // 9.0
print(calculateArea(Circle(1))); // 3.141592653589793
}
The pattern vocabulary behind case clauses
Every case is a pattern, and Dart 3 ships a full vocabulary: literal (case 1:), relational (case < 0: and >= 48 && <= 57), variable (case var n:), list ([int x, int y]), record ((int a, int b)), map, object, and logical-or (44 || 59) patterns. You have already met most of them in the examples above, and the pieces nest inside one case exactly as they do inside a list literal — list and record patterns destructure, object patterns pull fields off class instances, and a wildcard _ ignores what you do not care about.
One interaction to remember: constant patterns compare with ==, so custom equality from dart-equality-identical-hashcode.md directly influences which cases match. That is also why an enum case or a sealed subtype matches reliably — their values are constants with well-defined equality.
Choosing the right construct
| Construct | Best for | Produces a value? | Exhaustiveness |
|---|---|---|---|
if / else if / else | Simple boolean branches | No — statement | Not applicable |
if-case | One value against one pattern | No — statement | Not applicable |
switch statement | Several cases with multi-line bodies | No — statement | Checked |
switch expression | Mapping values onto a result | Yes — expression | Checked, _ or full coverage |
Prefer a switch expression when each case is a single expression that produces the result — it reads top to bottom, and the compiler enforces that every path returns. Prefer the statement form when cases need several statements, early exits, or continue labels, and reach for if/else when the branches are plain booleans rather than a value being classified. Branches pair naturally with the loop forms in dart-for-while-do-while-loops.md.
Takeaways
- An
ifcondition must be abool; Dart has no truthiness, so testisEmpty,== 0, or== nullexplicitly. if-casematches one value against one pattern, binds variables in the branch, and accepts an optionalwhenguard.- Non-empty switch cases never fall through; empty cases do, which is how cases share a body.
- Switch expressions use
=>bodies separated by commas, default to_, and must be exhaustive. - Guards fall through to the next case when false, and sealed classes plus enums give you compile-time exhaustiveness.
Takeaway: Reach for a switch expression whenever you are mapping a finite set of values to results — the compiler will then guarantee that no case is forgotten.