Sub topic 9
Why fields do not always promote, and how to work around it
Before you start
This chapter assumes that you know basic Dart variables, classes, methods, constructors, and nullable types such as String?. You should also understand that a nullable value may contain either a value or null.
Dart uses flow analysis to learn more about a variable after a check. For example, after checking that a local variable is not null, Dart can promote its type from String? to String.
Local variables can be promoted
A local variable is declared inside a function or method. If the variable cannot be changed unexpectedly, Dart can safely use the result of a null check.
int lengthOrZero(String? text) {
if (text == null) {
return 0;
}
// text is promoted from String? to String.
return text.length;
}
void main() {
print(lengthOrZero('Dart')); // 4
print(lengthOrZero(null)); // 0
}The return statement ends the null case. At the return text.length line, the remaining path guarantees that text is not null.
A local variable can still lose its promotion if you assign a nullable value to it later:
int example(String? text) {
if (text == null) {
return 0;
}
text = null;
// text.length would be invalid here.
return 0;
}Why a mutable field is different
A field is a variable stored inside an object. Other methods, code in another object, or a subclass may be able to change a mutable field. Because of that, Dart cannot always assume that the field still has the value it had during an earlier check.
class Box {
String? value;
int length() {
if (value == null) {
return 0;
}
// Dart may reject value.length because value is a mutable field.
// return value.length;
return value?.length ?? 0;
}
}The field might be read through a getter, and the getter could produce a different result each time. A mutable field can also be changed between two method calls. Refusing promotion prevents code from relying on an assumption that may no longer be true.
Copy the field to a local variable
The usual workaround is to read the field once into a local variable. Dart can promote that local variable after a null check.
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
box.value = null;
print(box.length()); // 0
}This pattern also gives consistent behavior. The method uses the same snapshot for the check and for the later read. It does not read the mutable field twice.
Other ways to make the value safe
Choose the option that matches the meaning of the field.
- Use a non-nullable field, such as
String, when the object must always have a value. - Use a
finalfield when the value is assigned once and never changes. - Use a local copy when the field is allowed to change.
- Use
?.or??when a fallback is the intended behavior. - Use
!only when your program has already guaranteed that the value is not null.
A final field is often easier for flow analysis because it cannot be reassigned after initialization. However, design the class around its real invariants rather than changing a mutable field to final only to satisfy the compiler.
> callout note > A null check proves something about a particular read. A local variable lets Dart connect that proof to the later use. A mutable field may be a different value by the time it is used.
Exercise
Exercise
Why is copying a nullable field to a local variable a common workaround?
Show answer
The local variable provides a stable value that Dart can promote after a null check, while a mutable field may change between the check and its use.