Magic Beyond Classes: Fluttering with Extension Methods in Dart đź’™

Enhance Code Reusability, Simplify Maintenance, and Level Up Your Flutter Development Process

Akansha Jain
Flutter Community
4 min readJul 30, 2023

--

Have you ever wondered whether there is a way to extend the functionality of libraries that we don’t control so that we can use those libraries in our Flutter projects while developing new features?

While Flutter provides a rich set of features out-of-the-box, sometimes developers need to extend the functionalities of existing classes without modifying their original source code. This is where extension methods come into play.

In this article, we will explore how to harness the full potential of this powerful tool to elevate our Flutter development process.

What are Extension Methods?

Extension methods are a new feature in Dart that allow developers to add new methods to existing classes. These methods can be called as if they were part of the original class, even though they are defined outside the class itself. These methods facilitate a clean and modular codebase by allowing developers to keep related functionalities separate from the main class definition.

In Dart, defining an extension method is straightforward. The extension keyword is used to declare an extension, followed by a name for the extension, and the target class to which the extension will be applied.

The body of the extension method is the same as the body of a regular method. The only difference is that the extension method can only be used on objects of the class that it is extending.

Here's the basic syntax:


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

In this syntax, the ExtensionName is the name of the extension and the TargetClassName 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.

How Do Extension Methods work?

In Dart, extension methods do not create a new object or introduce any implicit parameters. Instead, they are resolved statically at compile-time, and the extension methods become available as if they were part of the original class.

  1. Compile-time Resolution: Extension methods in Dart are resolved statically at compile-time, not at runtime. This means that when the code is compiled, the extension methods are “attached” to the target class, and the compiler understands that instances of the target class can now access these extension methods.
  2. No New Object Creation: When an extension method is called on an instance of the target class, Dart does not create a new object to handle the extension. The extension methods are not bound to the instance at runtime, nor is there any implicit parameter passed to the extension method.
  3. Accessing Properties and Methods: Inside the extension method, you can access the properties and methods of the target class directly. The extension methods behave just like regular static methods but appear to be part of the target class’s API.
  4. Importing and Scope: To use extension methods, you need to import the file containing the extension definition. Once imported, the extension methods are in scope and can be used on instances of the target class within that scope.

Let’s explore a practical example of how extension methods can be used in a Flutter application. Imagine you want to add some utility methods to the built-in int class:

int_extensions.dart

Once the extension is defined, you can use its methods on instances of the target class, as if those methods were part of the original class.

In the above example, we’ve defined an extension named IntExtensions for the int class, adding two utility methods: isEvenNumber() and isOddNumber().

To call an extension method, simply use the dot notation on an instance of the target class, followed by the method name. So, to use these extension methods in a Flutter widget:

main.dart

In this example, we’ve imported the int_extensions.dart file, which contains our extension methods, and used the extension methods on an integer variable myNumber. The output will display whether the number is even or odd.

It’s important to note that extension methods can only be called on instances of the target class when the extension is in scope. So, make sure to import the extension file wherever you want to use the extension methods.

Feel free to clone the repository from GitHub!

Extension methods in Flutter, built on the capabilities of Dart, offer a powerful mechanism to extend existing classes without modifying their original source code. They provide an elegant way to enhance code reusability, maintainability, and readability in our Flutter applications. By utilizing extension methods, we can create more modular and organized codebases, leading to a smoother development experience and easier maintenance in the long run.

Remember, while extension methods can be a valuable tool, it’s essential to use them thoughtfully and avoid overusing them. Always follow best practices and keep your codebase clean and maintainable.

If you are interested in learning more about extension methods, I recommend checking out the Dart documentation: https://dart.dev/language/extension-methods.

Happy coding with Flutter and extension methods! đź’™

Hope you enjoyed this article!

Doubts? Feel free to drop a message to me.

Don’t forget to connect with me on:

--

--

Akansha Jain
Flutter Community

Flutter Dev 💙 ▪️ Building solutions by leveraging the power of apps 🚀