days
0
-30
-3
hours
-1
-7
minutes
-3
-7
seconds
0
-3
search
Aiming high

Dart 2.7 adds support for extension methods & null safety preview

Sarah Schlothauer
dart
© Shutterstock / Pakhnyushchy

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.

Developed by Google, Dart is an open source, client-optimized language that can compile to JavaScript for the web or to ARM and x64 machine code for mobile, desktop, and backend.

Let’s look at the release highlights and the two big new features.

Extension methods

The most anticipated addition arriving in 2.7 is the introduction of extension methods.

SEE ALSO: DevOpsCon Munich 2019 takeaways: Everyone is in Ops now & more

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.

Analysis errors. Source.

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.

SEE ALSO: The Future of Angular and your Architectures with Ivy

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.

Download the newest SDK and see the latest features for yourself or check it out on GitHub.

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.

Author
Sarah Schlothauer

Sarah Schlothauer

All Posts by Sarah Schlothauer

Sarah Schlothauer is an assistant editor for JAXenter.com. She received her Bachelor's degree from Monmouth University and is currently enrolled at Goethe University in Frankfurt, Germany where she is working on her Masters. She lives in Frankfurt with her husband and cat. She is also the editor for Conditio Humana, an online magazine about ethics, AI, and technology.

Leave a Reply

avatar
400