Dart Null Safety
Dart Null Safety
null is a special value that means "no value".
In many languages, any variable can be null, and the program crashes when you use it by mistake.
Dart has null safety, which means that a normal variable can never be null.
If you try to put null in a String variable, the program does not run:
Result:
main.dart:2:17: Error: A value of type 'Null' can't be assigned to a variable of type 'String'.
String name = null;
^
Dart also checks that a variable has a value before you use it:
Result:
main.dart:3:9: Error: Non-nullable variable 'score' must be assigned before it can be used.
print(score);
^^^^^
Dart finds these mistakes before the program runs, so they cannot crash your program later.
Nullable Types
Sometimes a variable must be allowed to have no value.
Add a question mark ? after the type to make a nullable type.
A String? can hold a string or null:
Example
void main() {
String? nickname;
print(nickname);
nickname = 'Bobby';
print(nickname);
}
Try it Yourself »
Result:
null
Bobby
A nullable variable that has not been given a value is null.
You can use ? with any type, like int?, double? and bool?.
Why the Compiler Stops You
The compiler is the part of Dart that checks your code before it runs.
It does not let you use a nullable value as if it was a normal value.
In this example, int.tryParse() returns an int?, because the text might not be a number:
Result:
main.dart:3:13: Error: Operator '+' cannot be called on 'int?' because it is potentially null.
print(age + 1);
^
Dart does not know if age is a number or null, so it does not allow age + 1.
Null Checks and Type Promotion
The safe way is to check for null first, with != null.
Inside the if block, Dart knows that the value is not null:
Example
void main() {
int? age = int.tryParse('25');
if (age != null) {
print(age + 1);
} else {
print('Not a number');
}
}
Try it Yourself »
Result:
26
This is called type promotion.
After the check, Dart treats age as an int, not an int?.
The same code also works when the text is not a number:
Example
void main() {
int? age = int.tryParse('hello');
if (age != null) {
print(age + 1);
} else {
print('Not a number');
}
}
Try it Yourself »
Result:
Not a number
You will learn more about if and else in the If...Else chapter.
The ! Operator
If you are sure that a value is not null, you can add ! after it.
This is called the null assertion operator, and it turns an int? into an int:
Result:
26
But if the value is null, the program stops with an error when it runs:
Result:
Unhandled exception:
Null check operator used on a null value
Note: The compiler cannot protect you when you use !.
Use a null check instead whenever you can.
Default Values with ??
The ?? operator gives a default value when a value is null:
Example
void main() {
String? name;
String guest = name ?? 'Guest';
print('Hello, $guest');
}
Try it Yourself »
Result:
Hello, Guest
Dart has more operators for nullable values.
You can read about all of them in the Null-aware Operators chapter.