Flutter
Published in

Flutter

Dart sound null safety: technical preview 2

Announcing null-safe support for the Flutter framework

Why null safety?

Screenshot of the preceding code with null errors.
With null safety, Dart finds potential null errors in your code.

Null safety principles

  1. Non-nullable by default. Unless you explicitly tell Dart that a variable can be null, it will be considered non-nullable. We chose this as the default because we found that non-null was by far the most common choice in APIs.
  2. Incrementally adoptable. There’s a lot of Dart code out there. It will be possible to migrate to null safety at a time when you choose to, and then incrementally, part by part. It will be possible to have null-safe and non-null-safe code in the same project. We’ll also provide tools to help you with the migration.
  3. Fully sound. Dart’s null safety is sound. This means that we can trust the type system: if it determines that something isn’t null, then it can never be null. This enables compiler optimizations. Once you migrate your whole project and your dependencies to null safety, you reap the full benefits of soundness — not only fewer bugs, but smaller binaries and faster execution.

1. Non-nullable by default

Nullable variables

Being productive with null safety

2. Incrementally adoptable

Screenshot of the migration tool
The migration tool helps you interactively migrate your code to null safety.

3. Fully sound

The null safety roadmap

  1. Flutter experimentation with technical preview 2: This is today. Because we’ve successfully migrated the core Flutter framework to null safety, you’re able to try null safety to learn the new language feature and experiment with a small Flutter sample. If you’re a package author, you may also be able to do a trial migration, if you have a small dependency set that’s already been migrated by us. You need to pass an experiment flag, shouldn’t use it in production, and shouldn’t publish any migrated packages.
  2. Early package migration with beta: Later this year, we’ll complete performance tuning and have sufficient test coverage to give us confidence that the feature works as intended, and that backwards compatibility is solid. At that time we’ll publish a beta version of the feature, and you won’t need to pass the experiment flag. We hope to see package owners begin migration of their packages to null safety, which will give us one last round of validation that the feature is ready for a stable release.
  3. Production use with stable: Next, we’ll address feedback from beta, fix any remaining issues, and then publish to stable. It’s hard to state a concrete timeline for this, but we’re thinking early next year. Once the feature is stable, we hope to see lots of adoption of null safety, with null-safe apps published to stores and many null-safe packages published to pub.dev in stable versions.

Try it now

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Michael Thomsen

Product Manager working on Dart and Flutter. Helping developers is my passion!