Data Enumeration in Dart (original) (raw)
Last Updated : 07 Apr, 2025
Enumerated types (also known as **enumerations or **enums) are primarily used to define named constant values. The **enum keyword is used to define an enumeration type in Dart. The use case of enumeration is to store finite data members under the same type definition.
Declaring enums
enum variable_name{
// Insert the data members as shown
member1, member2, member3, ...., memberN
}
Let's analyze the above syntax:
- The **enum is the keyword used to initialize the enumerated data type.
- Enums in Dart are not classes but a special type that represents named constant values. However, starting from Dart 2.17, enums can have fields, methods and even implement interfaces, which makes them behave somewhat like classes.
- The data members inside the enumerated class must be separated by commas.
- Unlike some other languages, Dart does **not automatically assign integer values to enum elements. Instead, each enum value is an instance of the enum type, not an integer.
- Make sure not to use a semi-colon or comma at the end of the last data member.
Printing all the elements from the enum data class
**Example:
Dart `
// Dart program to print all the elements // from the enum data class
// Creating enum with name Gfg enum Gfg { // Inserting data Welcome, to, GeeksForGeeks, }
void main() { // Printing the value present in the Gfg for (Gfg geek in Gfg.values) { print(geek); } }
`
**Output:
Gfg.Welcome
Gfg.to
Gfg.GeeksForGeeks
**Note: Enum values print in the format
EnumType.valueName
, such asGfg.Welcome
. This is not because they lack quotes but because Dart automatically uses theirtoString()
method, which returns a string representation of the enum value.
Using switch-case to print result
**Example:
Dart `
// Defining an enumeration (enum) named Gfg with three values enum Gfg { Welcome, to, GeeksForGeeks }
void main() { // Assign a specific value from the enum to a variable 'geek' var geek = Gfg.GeeksForGeeks;
// Using a switch-case statement to check the value of 'geek' switch (geek) { case Gfg.Welcome: // Executes if 'geek' has the value Gfg.Welcome print("This is not the correct case."); break; case Gfg.to: // Executes if 'geek' has the value Gfg.to print("This is not the correct case."); break; case Gfg.GeeksForGeeks: // Executes if 'geek' has the value Gfg.GeeksForGeeks print("This is the correct case."); break; } }
`
**Output:
This is the correct case.
**Note: By default, enum values are instances of their enum type and are associated with integer indexes that start from 0, rather than strings.
**Limitation of Enumerated Data Type
- It cannot be subclassed or mixed in.
- It is not possible to explicitly instantiate an enum.
Conclusion
In Dart, enumerations (enums) offer a structured way to define a finite set of named constant values. Unlike traditional integer-based enums found in other programming languages, Dart's enums are actual instances of their type and do not have implicit numeric values.
Starting from Dart 2.17, enums can support fields, methods, and the ability to implement interfaces, making them more versatile for state management, switch statements, and representing fixed categories. However, it is important to note that enums cannot be subclassed, mixed in, or explicitly instantiated.
By utilizing enums, developers can improve code readability, maintainability, and type safety within Dart applications.