Photo by Daniele Franchi on Unsplash

Flutter 3: What are enums and what is new about it

Crizant Lai
CodeX
Published in
4 min readMay 17, 2022

--

Let’s continue our Flutter 3 series. Enums have got a new power in Flutter 3/Dart 2.17; in this article, I will talk about what enum is and its new feature in Flutter 3.

What’s Enum

Enum is the short form of “Enumerated type”, a set of named constants where all possible values are declared. Enum is not a unique feature of the Dart language but exists in most modern languages.

For example, if I’m going to develop a programmable interface for a smart lightbulb, I may do this:

const int LIGHT_BULB_STATUS_ON = 1;
const int LIGHT_BULB_STATUS_OFF = 0;

Then I will define a function to set the status of the lightbulb to on or off:

void setStatus(int status) {
// ...
}
// Usage:
setStatus(LIGHT_BULB_STATUS_ON);

This seems fine but one day a junior employee is adding new features to the codebase, and he sees that the setStatus function takes an integer parameter, then he somehow passed -1 to it, causing a runtime exception:

setStatus(-1);

It is not a good practice that he didn’t read through the codebase before doing any modification, but if the codebase is very large, who wants to read it? Is there a way to let us find the problem…

--

--

Crizant Lai
CodeX
Writer for

A TypeScript/Flutter developer who love technical stuffs.