Dart extension methods

Flutter · 104K views · 2019 · 6:14

Open on YouTube

Programming language designSoftware practice, open source & careersexplainer

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about how Dart extension methods work and how to use them.

Recommended by 2 people across 2 different months, 2019 to 2020.

What it is

A short official explainer from the Flutter team on Dart's new extension methods feature, which lets you add methods to existing types without modifying them. It is presented as a convenience feature and links out to an overview, a deeper article and code samples.

What people said

Recommends creativecreatorormaybenot · 2019-10 · Stack Overflow · on “How do I use Dart extension functions?”

There is an official video by the Flutter team [this video] about extension methods now. Static extension members Here is an intuitive example of how extension methods work: extension FancyNum on num { num plus(num other) => this + other; num times(num other) => this * other; } I simply extend num here and add methods to the class. This could be used like this: print(5.plus(3)); // Equal to "5 + 3". print(5.times(8)); // Equal to "5 * 8". print(2.plus(1).times(3)); // Equal to "(2 + 1) * 3". Note that the name FancyNum is optional and the following is valid too: extension on num {} When you use your extension in another file, you must give it a name. The extension above will make use of implicit

Recommends dev-aentgs · 2020-06 · Stack Overflow · on “Re-trying last item in stream, and continuing from there”

To some extent you will be able to modify, as a wrapper over existing functionality of that method. I haven't used them yet, so not much of experience. This video [this video] gives a very quick overview.

Cites Erick M. Sprengel · 2020-04 · Stack Overflow · on “Dart extension functions not found”

It was not your case, but for me, it worked after updating the Android Studio, Flutter plugin and Dart plugin. Maybe it'll help someone else. Version that extension methods are working: Android Studio 3.6.2 Flutter Plugin 192.7761 Dart Plugin 45.1.1 And another important point is that extension must be named. If it's not named it can't be used in other files: Private extension : extension on Banana { // ... } Public extension : extension BananaExtension on Banana { // ... } I found it in a comment on Dart extension announcement on YouTube [this video]