Skip to content
DeveloperMemos

Creating enum extensions in Dart

Dart, Flutter, Syntax2 min read

Some people reading this will probably think that I'm a little bit late to the party, but did you know that you can create extensions for enums in Dart?

Extensions as a language feature have actually existed since Dart 2.7. Flutter 2.2, that came out only a week or so ago, ships with Dart 2.13 - so extensions aren't exactly a new feature and I know that but I think that they do get overlooked. Some people also don't know that you can extend enums. Take the following enum as an example:

1enum Fruit {
2 apple,
3 orange,
4 banana
5}

A few different fruits! It's nothing that remarkable. But the interesting thing is there is actually no built-in way to add methods to the above enum in Dart. This is more simplistic than other languages, some offer this feature. One way around this is to make an extension for the enum. So for example, say that you wanted to assign a color to each fruit:

1extension FruitExt on Fruit {
2 Color get color {
3 switch (this) {
4 case Fruit.apple:
5 return Colors.red;
6 case Fruit.orange:
7 return Colors.orange;
8 case Fruit.banana:
9 return Colors.yellow;
10 default:
11 return Colors.red;
12 }
13 }
14}

Then you could expand on this and also add localized names for each fruit:

1extension FruitExt on Fruit {
2 ...
3
4 String get name {
5 switch (this) {
6 case Fruit.apple:
7 return "Apple";
8 case Fruit.orange:
9 return "Orange";
10 case Fruit.banana:
11 return "Banana";
12 default:
13 return "Apple";
14 }
15 }
16}

I wrote a full example of the code above in DartPad, you can play around with it here. It creates a ListView and then populates it with the fruits(with the font color set as each fruit's 'color').

Some extensions shortcomings

Dart extensions are pretty good and they help with bridging the gap for some missing features. That being said they aren't perfect and there are a couple of things I wish that they did better.

Static methods

The first thing is static methods. You can actually create static methods in an extension. The annoying thing is that they won't be directly applied to the class they are extending. Take this extension as an example:

1extension StringExt on String {
2 static String createSomething() => "Some words";
3}

After you declare this String.createSomething() isn't valid syntax. You need to call StringExt.createSomething() instead. I'm glad one of the options works but I would prefer to call String.createSomething() instead, using StringExt instead feels kind of clumsy and unintuitive.

Importing extensions

If your extension is in another file and you want to use it, you need to import that file. Sounds easy enough, right? It is but for some reason autocomplete doesn't pick up extensions yet. You can't just type StringExt and hit enter, you need to go to the top of the file and import the file containing the extension. I'm not sure if this is a language or an IDE issue but I do know that the author of supercharged gets around this by creating an empty class, something like:

1extension SuperchargedExt on Something {
2 ...
3}
4
5class Supercharged {}

I do this in some of my own projects too. It's a bit of a hack, but it works. By the way supercharged is great and you should definitely try it out!