Announcing sound null safety

Make your apps more stable and performant with Dart’s null safety

Filip Hracek
Jun 10 · 8 min read

By: Filip Hracek & Michael Thomsen

Today is a major milestone for the Dart team with the tech preview of our null safety work. Null safety helps you avoid a class of bugs that are often hard to spot, and as an added bonus enables a range of performance improvements. We’re now releasing an early tech preview, and we’re looking forward to your feedback.

This post describes the Dart team’s plans for rolling out null safety. It also explains what we mean by sound null safety and why that’s different from approaches that many other languages take.

Why null safety?

Dart is a type-safe language. This means that when you get a variable of some type, the compiler can guarantee that it is of that type. But type safety by itself doesn’t guarantee that the variable is not null.

Null errors are very common. A search on GitHub leads to thousands of issues caused by nulls in Dart code, and even more thousands of commits trying to fix those issues.

Try to see if you can spot the nullability problems in the following example code:

This function will certainly fail if called with null, but there’s a second case to consider:

The null safety feature makes this problem go away:

With null safety, you can reason about your code with more confidence. No more pesky runtime null dereferencing errors. Instead, you get static errors as you code.

Sound null safety

Dart’s null safety is sound. This means that Dart is 100% sure that the files list, and the elements in it, cannot be null in the above example. When Dart analyzes your code and determines that a variable is non-nullable, that variable is always non-nullable: if you inspect your running code in the debugger, you’ll see that non-nullability is retained at runtime. By contrast, some other implementations are unsound, and in many cases still need to perform runtime null checks. Dart shares sound null safety with Swift, but not very many other programming languages.

The soundness of Dart’s null safety has another welcome implication: it means your programs can be smaller and faster. Because Dart is really sure that files is never null, Dart can optimize. For example, the Dart ahead-of-time (AOT) compiler can produce smaller and faster native code, because it doesn’t need to add checks for nulls when it knows that a variable isn’t null.

We’ve seen some very promising preliminary results. For example, we saw a 19% performance improvement in a microbenchmark that emulates typical Flutter framework rendering patterns.

Design principles

Before starting the detailed design for null safety, the Dart team defined the following three core principles:

  • Non-nullable by default. Unless you explicitly tell Dart that a variable can be null, it will consider it non-nullable. We chose this as the default because we found that non-null was by far the most common choice in APIs.
  • Incrementally adoptable. There’s a lot of Dart code out there. It must be possible to migrate to null safety incrementally, part by part. It should 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.
  • Fully sound. As mentioned above, Dart’s null safety is sound. Once you migrate your whole project and your dependencies to null safety, you reap the full benefits of soundness.

Declaring variables with null safety

The core syntax is simple enough. Here are some non-nullable variables, declared in different ways. Remember, non-nullable is the default, so these declarations look like they do today, but their meaning changes.

Dart will make sure that you never assign null to any of the above variables. If you try to do i = null a thousand lines later, you’ll get a static analysis error and red squiggly lines, and your program will refuse to compile.

If you want your variable to be nullable, you can use ?, like this:

The above variables behave the same way that all variables do today.

You can use the ? syntax in other places, too:

But, once again, the dream is that you’ll almost never have to use ?. The vast majority of your types will be non-nullable.

Making null safety easier to use

The Dart team is trying hard to make null safety as easy to use as possible. For example, look at this code, which uses if to check for a null value:

Note how Dart is smart enough to realize that by the time we pass that if statement, the loudness variable cannot be null. And so Dart lets us call the clamp() method without jumping through hoops. This convenience is enabled by something called flow analysis: the Dart analyzer goes through your code as if it was executing it, figuring out additional information about your code automatically.

Here’s another example, which shows a case where Dart can be sure that a variable is non-null because we always assign a non-null value to it:

If you remove any of the assignments above (for example, by deleting the result = -1; line), Dart cannot guarantee that result will be non-null: you’ll get a static error and your code won’t compile.

Flow analysis only works inside functions. If you have a global variable or a class field, then Dart can’t guarantee when it will be assigned what value. Dart can’t model the flow of your whole application. For that reason, you can use the new late keyword when you know that a variable will be non-null before you first read it, but you can’t initialize it immediately.

Note that v is non-null, although it starts uninitialized. Dart trusts you that you won’t try to read v before it’s assigned a non-null value, and your code compiles without errors.

Null safety is backwards compatible

The Dart team has been working for more than a year to get null safety to tech preview. It’s the largest addition to the Dart language since we introduced Dart 2. Yet, it’s not a breaking change. Existing code can call into code that uses null safety, and vice versa. Even after null safety is available, it will be an optional feature that you can adopt when you’re ready. Your existing code will continue to run without change.

We recently migrated the Dart core libraries to fully use null safety. As an example of null safety’s backwards compatibility, we replaced the existing core libraries without any breakage in existing tests and test apps running in the Dart and Flutter test environments. We even rolled the new core libraries to our many internal Google customers, directly into their production code bases, without a hitch. We plan on migrating all of our packages and apps to use null safety when the feature launches, and we hope you’ll do the same. But you’ll be able to do it on your timeline, package by package, app by app.

The null safety roadmap

We plan to roll out null safety gradually, in three steps:

  1. Tech preview. This is launching today, and is available in Dart’s dev channel. See the “Try it now” section below for details. Things are still subject to change, so don’t use null safety in production code just yet. Do test things out and give us feedback, though!
  2. Beta release. Null safety will be available in Dart’s beta channel, and no longer behind an experimental flag. The feature will be very close to the expected final version. If you own a pub.dev package or plugin, this is when you can begin migrating, but you shouldn’t publish it as a stable version just yet.
  3. Stable release. Everyone will have null safety, and you’ll be encouraged to publish your migrated packages and plugins as stable versions. You should also migrate your production apps.

If all goes well, we plan to release null safety as a stable feature before the end of the year. Between now and then, we’ll add tooling to help you make your code null safe, including:

  • A migration tool to support you in automating many of the steps for upgrading existing packages and apps
  • Tags on pub.dev, so you can tell if a package supports null safety or not
  • An extension to the pub outdated command, with support for finding the latest versions of your dependencies that support null safety

Try it now

The quickest way to try null safety today is through nullsafety.dartpad.dev, a version of DartPad with null safety enabled. Open the Learn with Snippets drop-down to find a series of learning exercises that walk through the new syntax and basics of null safety.

You can also try null safety in small command-line apps. (We haven’t migrated larger frameworks like Flutter yet.) Start by downloading a copy of the dev-channel Dart SDK, and then get a copy of this sample Dart CLI app (GitHub repo, zip archive). The sample app’s README file has instructions for running the app with the null safety experiment flag. Other files in the sample provide launch configurations that enable debugging in VS Code and Android Studio.

We also have documentation, with plans to produce more:

We’re very happy to bring null safety to Dart. Sound null safety is a distinctive feature of Dart that helps you write less error-prone code and get better performance. We hope you’ll experiment with the feature in tech preview, and give us feedback through our issue tracker. Happy coding!

Dart

Dart is a client-optimized language for fast apps on any platform.

Filip Hracek

Written by

Developer and manager working on Google’s Dart programming language and Flutter SDK; gamebooks enthusiast; https://filiph.net

Dart

Dart

Dart is a client-optimized language for fast apps on any platform. Learn more at https://dart.dev.

Filip Hracek

Written by

Developer and manager working on Google’s Dart programming language and Flutter SDK; gamebooks enthusiast; https://filiph.net

Dart

Dart

Dart is a client-optimized language for fast apps on any platform. Learn more at https://dart.dev.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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