👨🏼‍💻Flutter| Enhanced Enums: The Powerful Feature of Dart 2.17(New feature of Flutter 3.0)
Hi Everyone👋 In this article, I will talk about “Enhanced Enum” which is came to Flutter with Dart 2.17.
Introduction
There was a good change on Enumeration type after Dart 2.17 was released. “Enhanced Enum” was added next to simple Enums. Of course, this provided to make our code more shorten.
Now let’s take a closer look at this issue with examples:
Simple Enum:
Before Dart 2.17 we could only use Enums like this:
enum ColorNames { red, green, blue }
We defined our Enum above. I want to define color based on incoming enum type. I can do it like this:
First, let’s write the extension that returns the required color according to the enum type:
Of course, we also want to get the “value” of the incoming color. We can get this directly from the extension we wrote above:
int colorValue=ColorNames.blue.getColorFromName().value;
But we want to get the value of the color directly according to the enum. Considering that we did not write the extension written above, we will need to write a extension like the one below:
As we can see, using a “simple enum” is a bit of a hassle. Let’s start to examine the “enhanced enum” together.
Enhanced Enum:
Dart also allows enum declarations to declare classes with fields, methods, and const constructors which are limited to a fixed number of known constant instances.
To declare an enhanced enum, follow a syntax similar to normal classes, but with a few extra requirements:
Instance variables must be
final
, including those added by mixins.All generative constructors must be constant.
Factory constructors can only return one of the fixed, known enum instances.
No other class can be extended as
Enum
is automatically extended.There cannot be overrides for
index
,hashCode
, the equality operator==
.A member named
values
cannot be declared in an enum, as it would conflict with the automatically generated staticvalues
getter.All instances of the enum must be declared in the beginning of the declaration, and there must be at least one instance declared.
Now let’s do the examples we made about the simple enum above with “enhanced enum”:
1.Enum Constructor: We want to return a Color in the enum. For this, let’s add a constructor to the enum and define a variable named “color”:
We don’t need to write an extra extension. Here’s how we get the color:
Color color=ColorNames.blue.color;
2.toString: Of course, let’s add a toString method to this enum and arrange it as we want:
Let’s add a new parameter to the constructor: name
Our final code will look like this:
For example:
String colorName=ColorNames.blue.toString();
Output:
Color name is: Blue
3.Add Mixin:
Color consists of three different values: red, green, blue. We want to get these values from the mixin. To do this, let’s first define a mixin:
Let’s use the mixin we created in the enum and return the components of the color in the enum:
For example:
int redCode=ColorNames.red.redCode();
int greenCode=ColorNames.red.greenCode();
int blueCode=ColorNames.red.blueCode();print("Red code: $redCode");
print("Green code: $greenCode");
print("Blue code: $blueCode");
Output:
Red code: 244
Green code: 67
Blue code: 54
4.Add Method:
Now let’s do something different and get the hex code inside the enum:
For example:
String hexCode=ColorNames.blue.hexCode();
print("HexCode: $hexCode");
Output:
HexCode: #2196f3
Conclusion:
The use of Enhanced Enum is very important to make our work easier. We need to start using this enum type without wasting time :)
I wish everyone a pleasant Flutter week :)