Null Safety
Null-Aware
?? and ?.
?? returns a fallback when the left side is null. ?. only calls the method if the receiver is non-null.
Program
Play the program to fall back to a default and safely read a length.
null_aware.dart
void main() {
String? name;
var display = name ?? 'default';
print(display);
name = 'Ada';
print(name?.length);
}
??
`a ?? b` is `a` when not null, otherwise `b`.
?.
`x?.method()` returns `null` when `x` is null instead of throwing.
safe access
Both operators avoid null dereferences without a manual `if`.