Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

dart.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.

Learn more
Sound null safety

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 anull value.

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:

dart
// 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:

dart
int?aNullableInt=null;

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 tonull at 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_override
dart 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:

  1. Check fornull safe versions of any packages you installed from pub.dev
  2. 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:

yaml
environment:sdk:'>=2.12.0 <3.0.0'

Migrating existing code

#
Warning

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 migrate

To 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:

Was this page's content helpful?

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.


[8]ページ先頭

©2009-2026 Movatter.jp