Dart Extensions in Flutter

Arathi Shankri
Flutter Community
Published in
3 min readDec 1, 2020

--

Courtesy: Dart Extensions

In this article, I would want to share the amazing concept of Dart Extensions that was introduced in Dart 2.7.0.

What are Extensions?

Extensions as the name suggests are the ability to add or extend the functionality of an existing class or a library. Using extensions one cannot modify the existing properties of that class but simply add more functionalities. Extensions are similar to “prototype” of javascript but are at compile time.

Where can you use it?

In Flutter, all our widgets are immutable, meaning once created we won’t be able to change the instance.

That’s where Dart Extensions come into play. Let's explore this with a real-time example;

In my gaming app, I needed primary, secondary, and warn themed buttons across multiple screens. Example screenshot on the left.

To achieve this without extensions, one could

  • Override the theme color on the buttons on every screen (because a button color set at the application theme level is applicable to every button, no distinguishes). This approach is clearly not maintainable.
  • Write a custom widget like I did to create the RaisedButton and override the color as required. As you can see in the code below, this approach has a…

--

--