The ability to add “Extension functions” is one of the most exciting feature of modern programming languages and guess what, Dart now supports extension functions. Extension functions were introduced in Dart 2.6.0. In this tutorial, you will learn everything you need to know about using extension functions in Dart.

What are dart extension members?

Imagine having the power to add a new method/member to any class that you wish, even if you don’t own the class. Image defining a method named isEmail on String class, and using it as if it were part of the class itself.

This is the kind of power that extension members give you.

Basic Usage

Say we have a Dog class.

Now you want to add a new method named bark on the Dog class without modifying the class itself. Let’s add an extension member.

You can now use the bark method as if it were part of Dog class.

As you can see the syntax to declare extensions is pretty easy. Use the keyword extension followed by the name of the extension(can be anything) and the class to add the extension on.

extension <extension_name> on <class_name>

When declaring an extension member(the bark method above), you can access the object that you are adding the extension on using this. That is how we are able to access the name property of Dog class. You cannot access anything that is private in Dog class.

Extension members on inbuilt classes

Extension members can be added to inbuilt classes as well. Let’s say we want to add a method named firstLetterUpperCase that returns the string itself but with first letter uppercase.

Remember, this in firstLetterUpperCase will refer to the string itself.

Use this method as if it were part of the String class.

You can also declare firstLetterUpperCase as a getter instead of a method.

Never miss a post from TheTechnoCafe

Extension members on generic classes

Extension members also work on generic classes. Let’s add a method on List<T> named deepEquals that will check if the contents of two lists are equal or not.

We will start off with checking if the other list is null or its length is not equal to the current list.

Use it like any other extension member.

Now you need to iterate over the lists and check if every item is equal.

This approach is fine but will fail when used with a list of objects.

We want the user to be able to decide if two items are equal or not. So let’s accept an optional argument that is a Function that will take in two items and return if they are equal or not.

User can now device if two dogs are equal or not. lol

That is it for extension members. Great work Dart Team! 🎉🎉 Looking forward for more features just like these.

Keep track of latest developments in dart lang via the CHANGELOG.

Read other articles on TheTechnoCafe:

Just Enough Dart for Flutter – Tutorial Series

Flutter Crash Course – Tutorial Series

Gurleen Sethi

How can I tell you about myself in just a few lines, to get to know me you can reach out to me, email, text, call, or any other method of communication you like. I am always there for you!


Leave a Reply

Your email address will not be published. Required fields are marked *

seventeen − six =