FLUTTERING DART
Fluttering Dart: Extension Methods
Add functionality to any type — even types you don’t control

Flutter projects can use both platform-specific and cross-platform code. The latter is written in Dart, and for building Flutter apps, some basic knowledge of Dart is required.
Fluttering Dart’s goal is to explore fundamental knowledge and unveil tips & tricks of the powerful programming language that brings Flutter to life.
The code examples can be tried out, and played with, using DartPad.
Extension Methods
Since the 11ᵗʰ of December 2019 the Dart language in its 2.7 version, received nice feature support: extension methods.
This enables us to add extra functionality to existing libraries APIs that we can’t modify or we don’t want to break.
We can extend with new methods, getters/setters, and even operators. The extensions get their own names which can help in resolving API conflicts.
The extensions will work on the static type of the receiver (either inferred or declared). So it works with the variables declared using the var keyword or any static type declaration, and it doesn’t work with the dynamic typed variables.
Implementation
First, you have to make sure that your Flutter project that uses the extension methods feature uses the right Dart SDK version. This has to be ≥ 2.7, the version in which Dart received support for this feature. You can check that inside your pubspec.yaml :
environment:sdk: ">=2.7.0 <3.0.0"If you have older projects you might have an older SDK version as the minimum required and as an effect, you will receive the following error:
This requires the 'extension-methods' experiment to be enabled.
Try enabling this experiment by adding it to the command line when compiling and running.In case you had the above issue, make sure that you restart your IDE after updating the pubspec.yaml file.
The syntax for the actual implementation is:
extension <extension_name> on <type> {
(<member_definition>)*
} Usage
For a few extensions, you can write all your extensions in one file, for example in extensions.dart . If the extensions are more complex or you want to keep everything clean you can create and extensions folder inside your project’s lib and follow a naming convention.
For example, for DateTime, you can name your file date_time_extension.dart . Note that the name of the file doesn’t have to reflect the name of any extension inside it (like it is the case with classes).
To use the extension methods that we implement, we simply import the extensions file where we want to use it.
After that, we get access, for our extended type, on the methods, getters/setters, and operators we added.
The extended members will appear in the code completion in our IDE alongside regular members.
Example
In my applications, I often work with DateTimes and sometimes I need to find how many units of time passed since a user action occurred. To do that for a DateTime userIn object without extension methods I would do the following:
Duration toNow = DateTime.now().toUtc().difference(userIn.toUtc());
print('${toNow.inMinutes} minutes since last user login');The extension method inside extensions.dart will look like this:
extension DateTimeExtended on DateTime {
Duration get toNow {
return DateTime.now().toUtc().difference(this.toUtc());
}
}And the above example for the same userIn DateTime will be:
import 'package:fluttering_dart/extensions.dart';// after importing the extensionsprint('${userIn.toNow.inMinutes} minutes since last user login');Note that you don’t have to declare a DateTimeExtended variable. The extension method toNow getter will be available to the DateTime typed variable that we have.
Also, I made sure the time is on the "same level" by getting the UTC value for the userIn and DateTime.now(), using the toUtc() method. This way we get rid of any possible Time Zone issues we might run into.
I find this feature quite nice and useful. It is something that comes in handy especially when you don’t have access to a library and you want to extend its out of the box functionality.
If you’re interested in finding out more about the Dart programming language you can do that here:
Tha(nk|t’)s all!






