Dart 3.11 is live!Learn more
Sound null safety
Information about Dart's null safety feature.
The Dart language enforces sound null safety.
Null safety prevents errors that result from unintentional access of variables set tonull.
For example, if a method expects an integer but receivesnull, your app causes a runtime error. This type of error, a null dereference error, can be difficult to debug.
With sound null safety, all variables require a value. This means Dart considers all variablesnon-nullable. You can assign values of the declared type only, likeint i=42. You can never assign a value ofnull to default variable types. To specify that a variable type can have anull value, add a? after the type annotation:int? i. These specific types can contain either anullor a value of the defined type.
Sound null safety changes potentialruntime errors intoedit-time analysis errors. With null safety, the Dart analyzer and compilers flag if a non-nullable variable has either:
- Not been initialized with a non-null value
- Been assigned a
nullvalue.
These checks allow you to fix these errorsbefore deploying your app.
Introduction through examples
#With null safety, none of the variables in the following code can benull:
// With null safety, none of these can ever be null.vari=42;// Inferred to be an int.Stringname=getFileName();finalb=Foo(); To indicate that a variable might have the valuenull, just add? to its type declaration:
int?aNullableInt=null;- To try some interactive examples, try out some of the null-safety orientated examples in theDart cheatsheet.
- To learn more about null safety, check outUnderstanding null safety.
Null safety principles
#Dart supports null safety using the following 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
Dart's null safety is 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.
Program-wide sound null safety lets Dart leverage these principles for fewer bugs, smaller binaries, and faster execution.
Dart 3 and null safety
#Dart 3 has built-in sound null safety. Dart 3 prevents code without it from running.
To learn how to migrate to Dart 3, check out theDart 3 migration guide. Packages developed without null safety support cause issues when resolving dependencies:
dart pub getBecause pkg1 doesn't support null safety, version solving failed.The lower bound of "sdk: '>=2.9.0 <3.0.0'" must be 2.12.0 or higher to enable null safety.Libraries incompatible with Dart 3 cause analysis or compilation errors.
dart analyze .Analyzing .... 0.6s error • lib/pkg1.dart:1:1 • The language version must be >=2.12.0. Try removing the language version override and migrating the code. • illegal_language_version_overridedart run bin/my_app.dart../pkg1/lib/pkg1.dart:1:1: Error: Library doesn't support null safety.// @dart=2.9^^^^^^^^^^^^To resolve these issues:
- Check fornull safe versions of any packages you installed from pub.dev
- migrate all of your source code to use sound null safety.
Dart 3 can be found in the stable channels for Dart and Flutter. To learn more, check outthe download page for details. To test your code for Dart 3 compatibility, use Dart 3 or later.
dart --version # make sure this reports 3.0.0-417.1.beta or higherdart pub get / flutter pub get # this should resolve without issuesdart analyze / flutter analyze # this should pass without errors If thepub get step fails, check thestatus of the dependencies.
If theanalyze step fails, update your code to resolve the issues listed by the analyzer.
Dart 2.x and null safety
#From Dart 2.12 to 2.19, you need to enable null safety. You can't use null safety in SDK versions earlier than Dart 2.12.
To enable sound null safety, set theSDK constraint lower-bound to alanguage version of 2.12 or later. For example, yourpubspec.yaml file might have the following constraints:
environment:sdk:'>=2.12.0 <3.0.0'Migrating existing code
# Dart 3 removes thedart migrate tool. If you need help migrating your code, run the tool with the 2.19 SDK, then upgrade to Dart 3.
You can migrate without the tool, but it involves hand editing code.
Dart code written without null safety support can be migrated to use null safety. We recommend using thedart migrate tool, included in the Dart SDK versions 2.12 to 2.19.
cd my_appdart migrateTo learn how to migrate your code to null safety, check out themigration guide.
Where to learn more
#To learn more about null safety, check out the following resources:
Unless stated otherwise, the documentation on this site reflects Dart 3.11.0. Page last updated on 2025-08-07.View source orreport an issue.