Sub topic 5
Null Checks and Type Promotion
Assume you know basic Dart variables, functions, classes, constructors, static types, and type inference. You should also be able to run a Dart program with dart run or DartPad.
A nullable value has a type such as String?. The question mark means the value can be either a String or null. A non-nullable String cannot be null, so Dart will not let you use a nullable value where a String is required until you check it.
Flow analysis after a null check
Dart uses flow analysis to track what is known at each point in a function. After checking that a local variable is not null, the analyzer promotes its type from String? to String.
int lengthOrZero(String? text) {
if (text == null) {
return 0;
}
// The return above means text is non-null here.
return text.length;
}
void main() {
print(lengthOrZero(null)); // 0
print(lengthOrZero('Dart')); // 4
}The early return is important. Every path that reaches text.length has already shown that text is not null.
You can write the same idea with an if and an else branch:
void printName(String? name) {
if (name != null) {
print(name.toUpperCase());
} else {
print('No name');
}
}Inside the first branch, name is promoted to String. Inside the else branch, Dart knows that name is null.
A check can also use a type test:
void printValue(Object? value) {
if (value is String) {
print(value.length);
}
}Inside the branch, value is promoted from Object? to String.
Checks that do not promote every variable
Promotion works best with local variables that cannot unexpectedly change. A mutable instance field can change between the check and the use, so Dart may reject this code:
class Box {
String? value;
int length() {
if (value == null) return 0;
// return value.length; // The field may no longer be known as non-null.
return value?.length ?? 0;
}
}Copy the field to a local variable when you want promotion:
class Box {
String? value;
int length() {
final current = value;
if (current == null) {
return 0;
}
return current.length;
}
}
void main() {
final box = Box()..value = 'hello';
print(box.length()); // 5
}The local variable current does not change after its declaration, so the null check remains valid.
Final fields and final local variables are easier for the analyzer to reason about. A getter can still produce a different result each time it is called, so storing its result in a local variable is often clearer and safer.
Dart 3 null-check patterns
Dart 3 also provides a concise null-check pattern. The pattern n? matches only a non-null value and binds it to n.
void greet(String? name) {
if (name case final value?) {
print('Hello, $value');
} else {
print('Hello, stranger');
}
}
void main() {
greet('Ada'); // Hello, Ada
greet(null); // Hello, stranger
}Use a normal if (value == null) check when it reads more naturally. Use a null-check pattern when you are already using pattern matching or want to bind a checked value in the condition.
Exercise
Why does copying a nullable field to a local variable often allow type promotion?
Show answer
A local variable can remain unchanged after the null check, while a mutable field or getter could produce a different value before it is used.