We as a consumer use many products in our day to day life. Be it a headphone or a car. But before the consumer, we are human first. We all have got some personality within us. We always try to pour our personality into the products that we use. Sometimes the manufacturer gives this freedom and if not people find their own ways to do it. 😎

After all its all about…

Image source: Google Search

Yes, we all do it to make the product truly ours.

Most of us reading this article might be playing the role of developer and we all have been doing the same thing knowingly or unknowingly. Sometimes at some point, we do a lot of code customization rather than writing a new one.

Let’s take an example of it. What if we want to perform any specific/new operation on String class and we want it to be used from many places inside the app. We would be creating a Helper or Utility class like this.

void main() {

print(StringUtil.isValidName('Pizza'));

}

class StringUtil {

static bool isValidName(String str) {

return !str.contains(new RegExp(r'[0–9]'));

}

}

It's all good. No harm in doing this. 👍

But look at the problem here. 😦

We may end up writing all helper methods like this in one single class. For better readability, we may also create another StringUtil class like shown in the example above and general methods in Util class only.

But calling our extra needed method from another class is something that doesn't give a real essence Object-oriented programming. Because we are used to perform an operation on the actual instance of an object like this.

myStr.toLowerCase();

myStr.isEmpty;

So how about if we could do this with our own custom methods?

Unfortunately 😦

You may not add your custom methods to the existing Dart files or any library(You don’t own the source code) and the Dart team or 3rd Party library does not know what you will need in advance.

Fortunately 😃

There is a solution to this 🤞

Presenting Extension Methods

The feature named Extension methods, Introduced in Dart 2.7 allows us to add new functionality to already available libraries. Although it does not allow us to modify existing code directly, It gives us the ability to customize a class in a way so that we can read and use it with ease.

We can also write an extension for getters, setters, and operators as well which is called Extension members. It has been given the name of ‘Extension methods’ so that developers coming from Kotlin, C# can easily co-relate it.

⚫️ Look at the syntax first-

extension <extension name> on <type> {

(<member definition>)*

}

Now the example -

extension ExtendedString on String {

bool get isValidName {

return !this.contains(new RegExp(r'[0–9]'));

}

} main() {

print('Pizza'.isValidName);

} Output: True

Short explanation -

With this, we have created our first extension that has a name as ExtendedString on a String class. Inside that, we have written getter which determines whether an instance of String contains a valid name or not using this keyword.

If you notice carefully we have actually written extended getter.

⚫️ Writing extended methods -

What if you want to concatenate a string with some predefined string. Let’s write an extended method for it.

extension ExtendedString on String {

String prefixWith(String prefix) {

return '$prefix $this';

}

} main() {

print('Pizza'.prefixWith('I love'));

} Output: I love Pizza

⚫️ Can we do the same with just an operator?

Yes. Here it is.