Extension methods in Flutter/Dart | Deep dive into Extension methods in dart


In this article, we’ll learn about extension methods in Dart. Perhaps you are wondering what that is, how it works, and why I need it. Then you’re in the right place right now. But before I continue, let me clarify something. The extension method is an advanced convenience feature. Extension methods add additional functionality to existing libraries’ APIs or our predefined methods. Several types of extensions can be defined, including methods, getters, setters, operators, etc.

In this way, you can create extension methods

To create an extension, follow these steps:

extension <extension name> on <type> {
(<member definition>)*
  • here extension name is your extension method name,
  • type is your data type name like, int, double, string, or maybe Widget as well. and member definition is your method. where you call this. (means your extension action perform to convert string to integer so on type is String)
  • member definition is your own method by which you perform return data(like your extension return int so your method return type is an integer).

Simple Method Example

So, let’s start with a simple example to know why extension methods are so useful for us.

Widget Method Example

The above example is for methods, can we do the same thing for our design, means many times we write the same code for many places for example making cards as rounded. So yes we can do this task as well with like as Utils class. let’s see its example as well,

it’s very useful, but you should keep some things keep in mind when you use extensions. let’s see what

  • dynamic variable does not support
  • hide extension as per requirement
  • import as: If two libraries have the same function name, then we can specify the library prefix by importing two libraries.
  • import show: It is used to import part of the library, only importing one name at a time.


The extension method allows developers to add custom functionality to an existing data type without creating a new derived type. Using extension methods, it is possible to write a method that can be called an instance method of an existing type. This extension feature can help you write better, cleaner code, which is one of Dart’s most valuable features.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shirsh Shukla

Shirsh Shukla

Mobile Application Developer | GDG Speaker | Technical Blogger at medium | community member at StackOverflow | Organizer @FlutterIndore