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.