Sub topic 11
Null-check patterns with Dart 3
This chapter assumes that you know basic Dart variables, functions, classes, constructors, static types, and type inference. You should also be able to run Dart code with dart run or DartPad.
The working model
A null-check pattern tests whether a value is not null. When the test succeeds, the pattern binds the value to a new variable with a non-nullable type.
The pattern has this shape:
final name?The ? belongs to the pattern. It means “match a non-null value.” It does not make the variable nullable.
void greet(String? name) {
if (name case final n?) {
print('Hello, $n');
} else {
print('Hello, stranger');
}
}
void main() {
greet('Ada');
greet(null);
}In the first call, n has type String, not String?. The else branch handles the null case.
This form combines a null check and a variable binding. Compared with a traditional check, it can make the relationship between the test and the new variable clearer:
void printLength(String? text) {
if (text == null) {
print(0);
return;
}
print(text.length);
}Both versions are valid. Use the pattern when it makes a larger pattern match easier to read.
Patterns in switch expressions
Null-check patterns are especially useful in switch statements and expressions. A switch expression must handle every possible value, so include a null case when the input is nullable.
String describe(String? value) {
return switch (value) {
final text? when text.isEmpty => 'an empty string',
final text? => 'text: $text',
null => 'no value',
};
}
void main() {
print(describe(null));
print(describe(''));
print(describe('Dart'));
}The first case matches a non-null string and adds a when guard. A guard is an extra Boolean condition that must also be true. The second case matches every other non-null string. The final case matches null.
The order matters. A general non-null pattern such as final text? would also match an empty string, so it must come after the more specific guarded case.
| Pattern | Values it matches | Bound variable |
|---|---|---|
| `null` | Only `null` | None |
| `final value?` | Any non-null value | Non-nullable |
| `final value? when condition` | Non-null values satisfying the condition | Non-nullable |
A diagram of the match
Common errors
A null-check pattern is not the same as the null-aware access operator ?..
user?.namereads a property only whenuseris non-null.user case final u?tests and bindsuserwhen it is non-null.
The pattern does not change the original variable. It creates a new binding:
void show(String? text) {
if (text case final value?) {
print(value.length);
}
// text is still String? here.
}Do not add ! to the bound variable. The pattern already proves that the variable is non-null:
if (text case final value?) {
print(value.length); // Correct
// print(value!.length); // Unnecessary
}For simple checks, if (text != null) remains a good choice. Choose a null-check pattern when you need pattern matching, a switch, a guard, or a clear local binding.
Exercise
What does if (email case final address?) guarantee inside its body when email has type String??
Show answer
It guarantees that address is a non-nullable String, so you can use address.length without !.