Honoring Asian Pacific American Heritage Month. See how.

Dart null safety

Goal & motivation

Fuchsia's Dart build transitioned from dartanalyzer to dart analyze in 2021. dart analyze supports checking sound null safety. This means runtime null-dereference errors can be turned into edit-time analysis errors.

Dart code written before Fuchsia code was checked for null safety needs to be migrated to take advantage of the null safety check. We would like to migrate all Dart sources to support null safety, since this will eliminate a whole class of runtime errors in Dart and Flutter applications.

Technical background

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;

See Dart's "Understanding null safety" for more details.

How to help

Picking a task

Search for Dart source files containing // @dart=2.9 or TODO(https://fxbug.dev/84961), all these files need to be migrated. For example you can code search for "// @dart=2.9" lang:dart.

Doing a task

NOTE: You may see import_of_legacy_library_into_null_safe from Dart analysis. This means one of the dependencies does not support null safety yet, and should be migrated first. If this happens to be a third-party dependency, we unfortunately have to wait for them to migrate first.

Make sure the Dart sources you are migrating are included in your build. Remove // @dart=2.9 and the TODO line above it, rebuild, then follow errors and suggestions from Dart analysis. See Dart's migration guide for more suggestions.

After you are done fixing all the errors from Dart analysis, add null_safety = true to the build target in the corresponding BUILD.gn file containing the sources you migrated.

Completing a task

When your change is ready, send it out for the corresponding owners for review. Preferable you can create a bug in Monorail to track the progress. If you do, make sure to mark it blocking the main bug 84961, and close it after your change has merged.



Reach out for questions or for status updates: