Dart Extensions Methods: String , Int , Boolean

Vinit MepaniVinit Mepani
2 min read

What are Extension Methods?

An extension method is a feature in Dart that allows us to add new methods to existing classes or interfaces without changing their original source code. These methods can be used as if they were part of the original class. In other words, extensions enable us to add functionality to a class or interface that they did not write and don’t control.

An extension method is a static method defined within a class that has a receiver parameter. The receiver parameter is the object to which the method is applied. The syntax for declaring an extension method is as follows:

  • Syntax

      extension ExtensionName on ClassName { 
          ReturnType methodName(ParameterType parameter) { 
              // implementation 
          } 
      }
    

    In this syntax, the ExtensionName is the name of the extension and the ClassName is the name of the class being extended. The ReturnType is the type of the value returned by the extension method. The methodName is the name of the method being added, and ParameterType is the type of the parameter being passed to the method.

Let's understand with String Example.

void main(){
  String name = "Vinit";
  print("HELLO ${name.greet()}");
}

extension StringExtension on String{
  String greet(){
    return this.toUpperCase();
  }
}

Let's understand with Int Example.

extension on int {
  // note: `this` refers to the current value
  int add() 
  {
    return this*2;
  }
}

void main()
{
int num = 30;
print('The Value of two number is ${num.add()}');
}

Let's understand with Double Example.

extension on double {
  // note: `this` refers to the current value
  double celsiusToFarhenheit() => this * 1.8 + 32;
  double farhenheitToCelsius() => (this - 32) / 1.8;
}

void main()
{
double tempCelsius = 20.0;
double tempFarhenheit = tempCelsius.celsiusToFarhenheit();
print('${tempCelsius}C = ${tempFarhenheit}F');
}

0
Subscribe to my newsletter

Read articles from Vinit Mepani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vinit Mepani
Vinit Mepani

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation. In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology. Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities. In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"