Hasnain Mirrani
3 min readAug 15, 2023

--

What is Enum in programming

in simple and easy words with examples in Dart / Flutter

an enumeration, often referred to as “enum” for short, is a way to define a set of named values that represent a collection of related constants. Enums make your code more readable and maintainable by giving meaningful names to these constants.

Here’s a simple explanation using a basic example in Python:

Let’s say you’re creating a program that deals with different days of the week. Instead of using numbers to represent each day, you can use an enum to give each day a meaningful name:

enum Color {
red,
green,
blue,
yellow,
}

void main() {
Color myFavoriteColor = Color.blue;

if (myFavoriteColor == Color.red) {
print("Your favorite color is red!");
} else if (myFavoriteColor == Color.green) {
print("Your favorite color is green!");
} else if (myFavoriteColor == Color.blue) {
print("Your favorite color is blue!");
} else if (myFavoriteColor == Color.yellow) {
print("Your favorite color is yellow!");
} else {
print("Invalid color choice");
}
}

In this example, we first define the Color enum with four possible values: red, green, blue, and yellow. We then create a variable myFavoriteColor and assign the value Color.blue to it.

The if and else if statements are used to compare the value of myFavoriteColor with each of the enum values. If the condition is met, the corresponding message is printed. If none of the conditions match, the else block prints "Invalid color choice".

another Example

const today = DayOfWeek.WEDNESDAY
enum DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY,
}

void main() {
DayOfWeek today = DayOfWeek.WEDNESDAY;

if (today == DayOfWeek.WEDNESDAY) {
print("It's Wednesday!");
}

Example 2: Let’s dive into another example to further illustrate how enums work in Dart. In this example, we’ll create an enum to represent different types of fruits: apple, banana, orange, and grape.

enum Fruit {
APPLE,
BANANA,
ORANGE,
GRAPE,
}

class FruitInfo {
final Fruit fruit;
final String color;
final String taste;

FruitInfo(this.fruit, this.color, this.taste);
}

void main() {
List<FruitInfo> fruits = [
FruitInfo(Fruit.APPLE, "Red/Green", "Sweet and Crisp"),
FruitInfo(Fruit.BANANA, "Yellow", "Soft and Sweet"),
FruitInfo(Fruit.ORANGE, "Orange", "Juicy and Tangy"),
FruitInfo(Fruit.GRAPE, "Purple/Green", "Sweet and Small"),
];

Fruit selectedFruit = Fruit.APPLE;

for (var fruitInfo in fruits) {
if (fruitInfo.fruit == selectedFruit) {
print("Selected fruit: ${fruitInfo.fruit}");
print("Color: ${fruitInfo.color}");
print("Taste: ${fruitInfo.taste}");
break;
}
}
}

In this Dart example:

  1. We’ve defined an enum named Fruit to represent different types of fruits.
  2. We’ve created a class FruitInfo that holds information about a fruit's name, color, and taste.
  3. In the main function, we've created a list of FruitInfo objects, each containing details about a specific fruit.
  4. We’ve initialized a selectedFruit variable to represent the fruit we want to get information about.
  5. We iterate through the list of FruitInfo objects, compare their fruit value with the selectedFruit, and print out information about the selected fruit.

This example showcases how enums can be used to categorize and manage different types of data in a structured and meaningful way, making the code more organized and easier to understand.

--

--

Hasnain Mirrani

Update the lattest and well explain All about Flutter make you from Zero to Hero in Flutter. follow https://www.linkedin.com/in/hasnain-mirrani-b47ab7131