Extensions in Flutter

Extensions in Flutter

In Flutter extensions allowed to you to add some new functionality to existing class without modified their original implementation. Extensions are particularly useful for adding helper method or computed properties to a class.

Code Syntax for using Extension Functionality in Flutter:

We have a class named Person as below:

class Person
{
int name ; 
int age;
Person(this.name, this.age)
}        

Now we will add a method in this Person class using Extension Functionality:

we use extension keyword to make the extension and on keyword to specify typed that being extended. Like in following example Person class has been extended using on keyword.

extension PersonDetail on Person
{
String getDetails()
{
return 'name is $name and age is $age'
}
}        

Let us call the this method in out main function:

var persondetail = Person(28,"arpan");
print(persondetail.getDetails());        

Output : name is arpan and age is 28

Note: You can not create the object of extension in Dart. Extensions are not type or classes.

You can extend the functionality of existing classes, even if you don't have access to their source code. The class or type comes from an external library or package (like Flutter's String, List, or Date Time classes) we don't have direct control over source code so extensions can be useful to add the functionality of these classes. Let we take a example with following code:

We will make the a extension function of String class to check that String is Palindrome or not

extension isStringPalindrome on String
{
  bool isPalindrome()
{
 int length = this.length;
  for (int i = 0; i < length ~/ 2; i++)
 {
  if (this[i] != this[length - 1 - i]) 
 {
      return false;
    }
  }
  return true; 
}        
 void main()
 {
String str= 'radar';
print(str.isPalindrome());
}        

Output: true

In the above example we have make a extension function "isPlaindrome()" of String class.

Note: The this keyword in Dart, especially within an extension function, refers to the instance of the type on which the extension is being applied. It provides access to the current object or value being extended.

Conclusion:

Extension functions enhance productivity by enabling developers to write clean, readable, and maintainable code while adhering to the principles of encapsulation and modular design. They are a valuable tool for extending and customizing the behavior of classes in a non-intrusive way.


要查看或添加评论,请登录

Arpan Bhatia的更多文章

  • Closures in Flutter

    Closures in Flutter

    A Closure is Function in Flutter that capture a value from its surroundings scope, even after scope exited. It is…

  • Higher Order Functions in Dart & Flutter:

    Higher Order Functions in Dart & Flutter:

    Higher Functions are the function that are used to make our code more concise, flexible and reusable. A higher order…

  • Mixin in Dart:

    Mixin in Dart:

    As the name suggest Mixin is use to mix it with some thing .In Flutter Mixin is way to reuse a code into multiple class…

    1 条评论
  • Kotlin Flows and Channels

    Kotlin Flows and Channels

    In modern software development, asynchronous programming has become a crucial aspect of building responsive and…

    1 条评论
  • StateLess Widget in Flutter

    StateLess Widget in Flutter

    Stateless widget simply means the widget that will be return in this class will not contain any state and it will never…

社区洞察

其他会员也浏览了