How Dart extensions can improve your code readability

Gabriele Feudo
3 min readJan 27, 2023

Dart is a programming language that is becoming increasingly popular among developers. One feature of Dart that can be particularly useful is its support for extensions.

Dart extensions allow developers to add new functionality to existing classes, without modifying the original source code. This can improve code readability, organization, and simplify complex code.

The Benefits of Using Dart Extensions

Improved code readability

By using extensions, you can add new methods and properties to existing classes in a way that makes the code more readable and intuitive. For example, an extension could add a capitalize() method to the String class, making it easy to format text in a consistent way throughout the codebase. For example:

"ciao!".capitalize(); // outputs: "CIAO!"
Date().toMyCustomFormat() // outputs a custom format: "Venerdì 27 gennaio"

// At this point you can also concat extension methods
Date().toMyCustomFormat().capitalize() // outputs: "VENERDÌ 27 GENNAIO"

Increased code organization

Dart extensions can be used to group related functionality together, making it easier to understand and maintain large codebases. For example, an extension could be created to group all text formatting methods together, making it easy to find and modify them as needed.

extension TextFormattingExtensions on String {
String capitalize() => this.toUpperCase();
String addEllipsis() => "${this}...";
String otherVeryUsefulExt() => ...
}

How to use Dart extensions

As you read before, to create a Dart extension, you need to use the extension keyword and specify the class that you want to extend. Then, you can add new methods, properties, and operators to the class just as you would in a normal class.

For example, if you want to add a method that returns the number of days until a certain date, you could create an extension to the DateTime class like this:

extension DateTimeExtension on DateTime {
int daysUntil(DateTime other) {
return other.difference(this).inDays;
}
}

To use the extension, you simply need to import it and use the new functionality as if it were part of the original class, for example:

final today = DateTime.now();
final birthday = DateTime.now().add(const Duration(days: 95));

print(today.daysUntil(birthday));

Real world scenarios

One example of using Dart extensions in real-world scenarios could be adding helper methods for working with dates to the DateTime class, like isWeekend(), isHoliday() or daysUntil(). Another useful way could be adding methods for working with image processing to the Image class, like crop(), resize() or filter(). Imagine how useful 🙂

Be aware!

When using Dart extensions, it’s important to keep in mind that they are not a replacement for good design. It’s always a good idea to use extensions sparingly, and to make sure that the new functionality is actually useful and improves the readability of the code.

Moreover, it’s important to use meaningful and descriptive names for your extensions and the methods and properties that you add. Try to make your extensions as small and focused as possible, and avoid adding functionality that is not directly related to the class being extended.

Do you want to read more?

If you liked this article and want to stay up to date with both advanced and beginner Flutter tutorials, follow me here on Medium and on LinkedIn.

Psst! If you want support my work here on Medium, feel free to buy me a coffee 😇

See you soon! 👨🏻‍💻

Gabriele

--

--

Gabriele Feudo

Co-founder & CEO @ Keplero.ai | Constantly pursuing my goal of improving people’s life with innovation and knowledge sharing.