Extension method in dart(flutter).

In this article, You will get answers on what is the extension method, how to create an extension method, and why we create it.

Extension method in dart(flutter).

Hello,

Dart has one feature called the extension method and you might have already heard that somewhere as many languages have it these days.

If you are completely new to extension methods, then might be you have questions like What is the extension method? Why do I need an extension method, and how do I use it?

But before I answer these questions, I want to clarify that the extension method is an advanced convenient feature, so it's completely optional to learn and use. There is nothing you can not do without an extension function. (But of course, there are some advantages to using it).

So Let's start with,

What the problem extension method is trying to solve:

Imagine you are building an app that is used across the world and you want to show the price of the product in different currencies for different regions but APIs gives us price in dollar only.

We're a developer so we know what to do, we'll make a helper function

convertToRupee(price) convertToEuro(price)

That's great, nothing wrong here.

But we are not usually talking or thinking like this, we always see an object first and then perform any action on it.

print(price.inRupee)
print(convertToRupee(price))

The first one of course more readable, right?

So when we are using any third-party library and you have to do some operations like the above, but we don't have control over that class because it is from the library, so we can't actually write a method for that class, and there extension method comes to rescue.

You can extend the method to any class using the extension method.

Let's see, how can we write the extension method:

extension <extension name> on <type> {
  (<member definition>)*
}

Let's write our first extension method for converting the price(which is in dollars) into rupees.

extension on double{
  double get inRupee{
    return (this * 80);
  }
}

Done! We have just written our first extension method on double, we can now use it anywhere in the application, but wait for a second, you might have a question in your mind by reading this line return (this * 80) from where does "this" keyword came

We can access instance from which the extension method is called via the 'this' keyword.

Now let's see how can we use this extension method,

void main() {
  double price = 10;
  print(price.inRupee);
  // Output: 800
}

Full code:

void main() {
  double price = 9;
print(price.inRupee);
}

extension on double{
  double get inRupee{
    return (this * 80);
  }
}

More examples :

Do the sum of integer values in the list

void main() {
  List<int> numbers = [1,5,3,10];
  print(numbers.sum);
  //Output : 19
}

extension on List<int>{
  int get sum{
    int sum = 0;
    this.forEach((number) => sum += number);
    return sum;
  }
}

Check string has some value or is null/empty.

void main() {
  String? name = "Bhoomit";
  name.isNullOrEmpty ? print("-") : print(name);
}

extension on String?{
  bool get isNullOrEmpty{
    return this==null || this!.isEmpty;
  }
}

Feel free to comment if you have any questions or suggestions.