Sub topic 3
What “Sound” Means and What the Compiler Guarantees
This chapter assumes 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.
The problem sound null safety solves
A null value means “there is no value.” Before sound null safety, a variable that looked like a String could still contain null. Code could then fail far from the place where the invalid value entered the program.
String firstLetter(String name) {
return name[0];
}With sound null safety, the type String means a real, non-null String. The type String? means either a String or null.
String firstLetter(String name) {
return name[0];
}
void main() {
print(firstLetter('Dart'));
// print(firstLetter(null)); // Compile-time error
}The compiler rejects the invalid call before the program runs.
What “sound” means
In this context, sound means that the type system’s conclusions match the values that can occur at runtime, within code checked by Dart’s null-safety rules.
If Dart accepts an expression as a non-nullable String, the expression will not produce null during normal execution. Therefore, the compiler lets you use it without repeated null checks.
String? name = 'Ada';
if (name != null) {
print(name.length); // name is promoted to String
}The check changes what the compiler knows about name inside the block. If the value were null, the block would not run.
| Type | Values allowed | Safe to use as a non-null value? |
|---|---|---|
| `String` | Strings only | Yes |
| `String?` | Strings or `null` | No, check first |
| `Never` | No reachable values | Not applicable |
Guarantees and boundaries
Sound null safety gives useful guarantees:
- A non-nullable variable cannot be assigned
nullby checked Dart code. - A non-nullable parameter cannot receive
nullfrom a checked caller. - A non-nullable return type cannot return
nullfrom a checked function. - The compiler identifies many invalid uses before execution.
- After a successful null check, flow analysis can treat a local value as non-nullable.
These guarantees do not mean every null-related failure is impossible. The compiler cannot prove facts that your program does not express. A value from external input, a database, or a map lookup may still be absent.
int readAge(Map<String, int> ages, String user) {
final age = ages[user]; // int?, because the key may be absent
if (age == null) return 0;
return age;
}A cast or null assertion can also override the type checker. Those operations move responsibility from the compiler to you.
flowchart TD
A["Value has type String?"] --> B{"Check for null"}
B -->|null| C["Handle absence"]
B -->|non-null| D["Flow analysis promotes to String"]
D --> E["Use String members safely"]The role of `!`
The null assertion operator, !, tells Dart, “I guarantee this value is not null.”
String requireName(String? name) {
return name!;
}This compiles, but it is not a stronger guarantee. If name is null, the program throws at runtime. Prefer a check, a default value, or an API design that makes absence explicit.
Exercise
Exercise
Why does Map<String, int> ages produce an int? when you write ages['bob'], even though the map’s values are declared as int?
Show answer
The key might not exist. The map stores non-nullable int values for present keys, but a missing lookup returns null, so the lookup expression has type int?.