Dart If-Case
An if-case statement tests one value against one pattern. Its branch runs only when the value matches the pattern.
One value, two alternatives
The value 2 matches the second alternative of the logical-or pattern. The value 5 matches neither alternative, so its branch is skipped and the else branch runs.
void main() {
var v = 2;
if (v case 1 || 2) {
print('small number');
}
var w = 5;
if (w case 1 || 2) {
print('small number');
} else {
print('not small');
}
}The null-check pattern
The question mark after var n makes the pattern match only a non-null value. The filled variable matches and binds 7, and the empty variable falls to the else branch.
void main() {
int? value = 7;
if (value case var n?) {
print('has value $n');
}
int? empty;
if (empty case var n?) {
print('has value $n');
} else {
print('nothing there');
}
}The cast pattern
The cast pattern binds hello as a String and prints it. The number fails its cast, the catch block runs, and its line prints instead.
void main() {
Object raw = 'hello';
if (raw case var text as String) {
print(text);
}
Object other = 42;
try {
if (other case var text as String) {
print(text);
}
} on TypeError {
print('not text: $other');
}
}A map pattern in an if-case
The map pattern asks for a name key and an admin role in one test. Ana matches and her bound name prints, and Ben holds a guest role, so his test falls to the else branch.
void main() {
var user = {'name': 'Ana', 'role': 'admin'};
if (user case {'name': var name, 'role': 'admin'}) {
print('admin: $name');
}
var guest = {'name': 'Ben', 'role': 'guest'};
if (guest case {'name': var name, 'role': 'admin'}) {
print('admin: $name');
} else {
print('not admin: Ben');
}
}Example
void main() {
var settings = <String, Object>{'theme': 'dark', 'fontSize': 14, 'beta': true};
if (settings case {'theme': var theme as String}) {
print('theme: $theme');
}
if (settings case {'fontSize': var size as int}) {
print('font size: $size');
}
if (settings case {'missing': var value}) {
print('missing: $value');
} else {
print('no missing key');
}
}Key points
- An if-case tests one value against one pattern and runs its branch only on a match.
- A logical-or pattern matches when the value equals any one of its alternatives.
- The null-check pattern adds a question mark after a variable pattern and matches only a non-null value.
- A cast pattern casts the matched value to a named type and binds the result to a variable.
- A cast pattern throws when the cast fails, so it does not quietly miss a value of the wrong type.
- A map pattern matches a map that holds the named keys, and it binds the values stored under them.
- Variables bound by the pattern exist only inside the branch, so code after the if statement cannot read them.
In this example, the settings map is tested three times with if-case. The theme test casts its value to a String and prints dark. The font size test casts its value to an int and prints 14, and the missing key fails its map pattern, so the else branch prints its line.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท DartPad
Exercise
Where can code read a variable bound by an if-case pattern?