avatarConstantin Stan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2615

Abstract

e minimum required and as an effect, you will receive the following error:</p><div id="16eb"><pre>This requires <span class="hljs-keyword">the</span> <span class="hljs-string">'extension-methods'</span> experiment <span class="hljs-built_in">to</span> be enabled. Try enabling this experiment <span class="hljs-keyword">by</span> adding <span class="hljs-keyword">it</span> <span class="hljs-built_in">to</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">command</span> <span class="hljs-title">line</span> <span class="hljs-title">when</span> <span class="hljs-title">compiling</span> <span class="hljs-title">and</span> <span class="hljs-title">running</span>.</pre></div><p id="01d8">In case you had the above issue, make sure that you restart your IDE after updating the <code>pubspec.yaml</code> file.</p><p id="f099">The syntax for the actual implementation is:</p><div id="5c10"><pre>extension <span class="hljs-variable"><extension_name></span> <span class="hljs-keyword">on</span> <span class="hljs-variable"><type></span> { (<span class="hljs-variable"><member_definition></span>)* } </pre></div><h2 id="cd49">Usage</h2><p id="4b46">For a few extensions, you can write all your extensions in one file, for example in <code>extensions.dart</code> . If the extensions are more complex or you want to keep everything clean you can create and <b>extensions</b> folder inside your project’s <b>lib</b> and follow a naming convention.</p><p id="1e74">For example, for DateTime, you can name your file <code>date_time_extension.dart</code> . 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).</p><p id="9861">To use the extension methods that we implement, we simply import the extensions file where we want to use it.</p><p id="a884">After that, we get access, for our extended type, on the methods, getters/setters, and operators we added.</p><p id="bc81">The extended members will appear in the code completion in our IDE alongside regular members.</p><h2 id="d07f">Example</h2><p id="3f4e">In my applications, I often work with <b>DateTime</b>s and sometimes I need to find how many units of time passed since a user action occurred. To do that for a DateTime <b>userIn</b> object without extension methods I would do the following:</p><div id="ef74"><pre>Duration <span class="hljs-keyword">to</span>Now = DateTime.now().<span class="hljs-keyword">to</span>Utc().difference(<span class="hljs-keyword">user</span>In.<span class="hljs-keyword">to</span>Utc()); print('${<span class="hljs-keyw

Options

ord">to</span>Now.<span class="hljs-keyword">in</span>Minutes} minutes since last <span class="hljs-keyword">user</span> login');</pre></div><p id="79ae">The extension method inside <code>extensions.dart</code> will look like this:</p><div id="e6f7"><pre><span class="hljs-keyword">extension</span> DateTimeExtended <span class="hljs-keyword">on</span> <span class="hljs-built_in">DateTime</span> { <span class="hljs-built_in">Duration</span> <span class="hljs-keyword">get</span> toNow { <span class="hljs-keyword">return</span> <span class="hljs-built_in">DateTime</span>.now().toUtc().difference(<span class="hljs-keyword">this</span>.toUtc()); } }</pre></div><p id="dbff">And the above example for the same <b>userIn </b>DateTime will be:</p><div id="0bcd"><pre><span class="hljs-keyword">import</span> <span class="hljs-string">'package:fluttering_dart/extensions.dart'</span>;</pre></div><div id="3b5e"><pre><span class="hljs-comment">// after importing the extensions</span></pre></div><div id="0fab"><pre>print('${userIn.toNow.inMinutes} minutes since last <span class="hljs-keyword">user</span> <span class="hljs-title">login</span>');</pre></div><p id="4b17">Note that you don’t have to declare a <b>DateTimeExtended</b> variable. The extension method <b>toNow</b> getter will be available to the<b> DateTime</b> typed variable that we have.</p><p id="e953">Also, I made sure the time is on the "same level" by getting the UTC value for the <b>userIn</b> and <b>DateTime.now()</b>, using the <b>toUtc()</b> method. This way we get rid of any possible Time Zone issues we might run into.</p><p id="7c20">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.</p><p id="388d">If you’re interested in finding out more about the Dart programming language you can do that here:</p><div id="7608" class="link-block"> <a href="https://levelup.gitconnected.com/fluttering-dart/home"> <div> <div> <h2>Fluttering Dart</h2> <div><h3>Explore Dart fundamentals and unveil tips & tricks of the powerful programming language that brings Flutter to life.</h3></div> <div><p>levelup.gitconnected.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*f2mfww0JrshKV_aR)"></div> </div> </div> </a> </div><p id="9c98">Tha(nk|t’)s all!</p></article></body>

FLUTTERING DART

Fluttering Dart: Extension Methods

Add functionality to any type — even types you don’t control

Photo by Hal Gatewood on Unsplash

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 extensions
print('${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!

Dart
Flutter
Programming
Fluttering Dart
Recommended from ReadMedium