Sub topic 1
Why Dart Null Safety Exists
Dart programs often receive values from places that do not always have an answer: a missing database field, an empty form input, a failed lookup, or an object that has not been created yet. The special value null represents “no value.” Null safety makes the possibility of no value visible in a variable's type.
Prerequisites
This chapter assumes that you know basic Dart variables, functions, classes, constructors, and static types. You should also be able to run a Dart program with dart run or DartPad.
The problem with null references
Without null safety, any reference could potentially contain null. Code could then call a method or read a property on that value:
void main() {
String name = getName();
print(name.length);
}
String getName() {
return ''; // A real application might return null here.
}If getName returned null, the property access would fail at runtime. The failure might happen far from the code that introduced the missing value. It could appear only for a particular user, record, or network response.
These failures are commonly called null reference errors. They are difficult to find because the program compiles successfully and fails only when a particular path runs.
Null safety changes the question from “Could this value be null?” to “Does this type allow null?”
Non-nullable by default
In Dart with sound null safety, String means a value that must not be null. String? means a value that can be either a String or null.
void main() {
String name = 'Ada';
String? nickname;
// name = null; // Compile-time error.
print(nickname); // null
}The compiler rejects name = null because the declared type does not permit it. The question is answered before the program runs.
Use a nullable type when “missing” is a valid state. Use a non-nullable type when the program requires a value. This distinction documents the contract between parts of the program.
| Type | Allowed values | Typical meaning |
|---|---|---|
| `String` | String values only | A required name |
| `String?` | String values or `null` | An optional nickname |
| `int` | Integer values only | A required count |
| `int?` | Integer values or `null` | A count that may be unknown |
A nullable value cannot be used as though it were definitely present. The compiler requires you to handle the missing case. Later chapters cover the operators and checks used for that handling.
What sound null safety guarantees
“Sound” means that the type system's conclusions match the values the running program can observe, within Dart's supported safety rules. If an expression has type String, Dart can rely on it not being null. Therefore, calling length on that expression does not need a null check.
int lengthOf(String value) {
return value.length;
}
void main() {
print(lengthOf('Dart')); // 4
}A nullable argument cannot be passed directly:
void main() {
String? value;
// print(lengthOf(value)); // Compile-time error.
}
int lengthOf(String value) {
return value.length;
}The error forces the caller to decide what to do when value is null. It might provide a fallback, return early, show an error, or ask for input. That decision belongs in application logic rather than being left to an accidental runtime crash.
Why this improves design
Null safety makes missing data explicit at API boundaries. A function that accepts String communicates that callers must provide a value. A function that accepts String? communicates that the function knows how to handle absence.
This clarity helps in three ways:
- The compiler finds unsafe uses during development.
- Types describe required and optional data.
- Error handling happens near the point where the missing value is understood.
Exercise
Should a user profile's required display name be declared as String or String? if the application must always show a name?
Show answer
Declare it as String. If the data source can omit the name, handle that omission before constructing the profile, for example by supplying a valid fallback or reporting invalid data.