dart extension is a new feature introduced in the dart 2.7version.

It adds new functionality to an existing library or type.

It is very difficult to modify the APIs written by other developers. You can still add extension features.

Dart extension Syntax.

extension keyword is used to add a new class to Existing Classname.

extension NewClassName on ExistingClassName{
 // methods.
 void functionname(){
     
 }
}

We can access the methods or functions using a name with an existing classname.

ExistingClassName.functionname();

Extension Methods in dart

We can write methods for existing libraries or types.

The string is a dart core type which we don’t have control to modify.

Added the extension class and defined a new method.

this inside method is used to get the current value.

void main() {
  print("43".parseInt());
}

extension NewClass on String {
  int parseInt() {
    return int.parse(this);
  }
}