Dart 2.7 adds support for extension methods & null safety preview
Dart version 2.7 adds a feature that has been a long time in the making: extension methods. Extension methods allow you to add functionality to any type. Developers can also opt-in and check out the preview for null type safety. Take a brief tour of the newest stable release, what it includes, and what features hit the bullseye.
With the release of Flutter 1.12, Dart 2.7 also arrives, adding a new long-awaited feature: extension methods.
Was 2019 the year of Dart? In January 2019 our readers voted and they said that they wanted to hear about Dart over other programming languages. Meanwhile, GitHub’s State of the Octoverse revealed Dart as the fastest-growing language according to the number of contributors.
Announcing the stable release of the Dart 2.7 SDK!
Support for extension methods, and technical previews for null safety, and a new package for handling strings with special characters.
— Dart Language (@dart_lang) December 11, 2019
Let’s look at the release highlights and the two big new features.
The most anticipated addition arriving in 2.7 is the introduction of extension methods.
From the release announcement post by Product Manager Michael Thomsen:
Dart 2.7 adds a long-requested, powerful new language feature: extension methods. These enable you to add new functionality to any type — even types you don’t control — and have the brevity and auto-complete experience of regular method calls.
According to Thomsen, extension methods are resolved and dispatched statically and can have type variables. Extension methods help add functionality to libraries and work just as fast as calling static functions. Using extension methods is as simple as importing the library they are contained in.
View the guide to learn more about them and their API conflicts. Lasse Reichstein Holst Nielsen, one of the creators of the features, explains in-depth how to use extension methods in an article titled Dart Extension Methods Fundamentals.
Previewing null safety
While it isn’t fully stable yet, null safety gets an update; null safety static analysis is now available for preview. Get some hands-on experience and test out some samples in the new null safety playground in DartPad.
With null safety, Dart will show any errors related to declaring variables as null without running the code. From there, you can make edits, fix the mistakes, and achieve safe code.
Once null safety reaches beta, devs will be able to use it in their apps and packages.
Keep an eye out for upcoming new features. According to the announcement post, work is currently underway on porting the Dart core libraries, adding a migration tool, and completing nullable and non-nullable references.
This preview is opt-in, so you can start using it whenever you are ready.
Check out 2.7
How can Dart top its performance in 2019? The upcoming plans for 2020 include stable versions of dart:ffi and of course, further work on null safety.
It is also included in Flutter 1.12 and in the newest version the in-browser editor DartPad, which includes all of Flutter’s core libraries.