Sound null safety

The Dart language now supports sound null safety!

When you opt into null safety, types in your code are non-nullable by default, meaning that variables can’t contain null unless you say they can. With null safety, your runtime null-dereference errors turn into edit-time analysis errors.

With null safety, all of the variables in the following code are non-nullable:

// In null-safe Dart, 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 might have the value null, just add ? to its type declaration:

int? aNullableInt = null;


Null safety principles

Dart null safety support is based on the following three core design principles:

  1. Non-nullable by default.
  2. 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.

  3. Incrementally adoptable.
  4. You choose what to migrate to null safety, and when. You can migrate incrementally, mixing null-safe and non-null-safe code in the same project. We provide tools to help you with the migration.

  5. Fully sound.
  6. Dart’s null safety is sound, which enables compiler optimizations. If the type system determines that something isn’t null, then that thing can never be null. Once you migrate your whole project and its dependencies to null safety, you reap the full benefits of soundness — not only fewer bugs, but smaller binaries and faster execution.