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

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

void printLengths(List<File> files) {
  for (var file in files) {
    print(file.lengthSync());
  }
}

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

void main() {
  // Error case 1: passing a null to files.
  printLengths(null);  // Error case 2: passing list of files, containing a null item.
  printLengths([File('filename1'), File('filename2'), null]);
}

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.

#dart #flutter #mobile-apps

Announcing Sound Null Safety
3.25 GEEK