Sound null safety
An introduction to null safety in Dart.
The Dart language enforces sound null safety,
making it impossible to unintentionally access a member on a null value.
In Dart, types are non-nullable by default. Variables of non-nullable types must be initialized and can only be assigned non-null values.
The Dart analyzer and compilers catch unsafe usage
of potentially null values at edit time,
turning what would be runtime errors in other languages
into analysis errors you can fix before deploying.
Examples
#None of the variables in the following code can be null:
// None of these can ever be null.
var i = 42; // Inferred to be an int.
String name = getFileName();
final b = Foo();
To indicate that a variable can have the value null,
add ? to its type declaration:
int? aNullableInt = null;
- For interactive examples, try the Dart cheatsheet.
- To learn more about null safety, check out Understanding null safety.
Null safety principles
#Null safety in Dart is built on two core design principles:
- Non-nullable by default
-
Unless you explicitly tell Dart that a variable can be null, it's considered non-nullable. This default was chosen after research found that non-null was by far the most common choice in APIs.
- Fully sound
-
If the type system determines that a variable or expression has a non-nullable type, it's guaranteed that it can never evaluate to
nullat runtime.
Together, these principles result in fewer bugs, smaller binaries, and faster execution.
Historical migration resources
#
Dart has enforced sound null safety since Dart 3, released in May 2023.
If you still need to migrate your apps or packages to null safety,
check out the archived documentation in the
dart-community/migrate-to-null-safety
repository.
Unless stated otherwise, the documentation on this site reflects Dart 3.11.0. Page last updated on 2026-02-26. View source or report an issue.