![]() |
VOOZH | about |
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.
enum variable_name{
// Insert the data members as shown
member1, member2, member3, ...., memberN
}
Let's analyze the above syntax:
Example:
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.
Example:
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.
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.