What are enumerated types in Dart?
Enumerated types (often called enums) specify named constant values. In Dart, the enum keyword declares an enumeration type. Enumeration is used to hold finite data members with the same type of specification.
Syntax
enum enum_name{
// data members
member1,
member2,
....,
memberN
}
where:
- The
enumkeyword creates an enumerated data type. enum_namedefines the name of the enumerated type.- Commas must be used to separate the data members within the enumerated class.
- Each data member is given a number bigger than the preceding one, beginning with
0. (by default).
Note: In the conclusion of the last data member, avoid using a semicolon or comma.
Code
The following code prints all the elements from the enum data class:
// Create enumenum Shot {// data membersDart,Enumeration,types}void main() {Shot educativeShot;// Print the value in Shot classfor (educativeShot in Shot.values) {print(educativeShot);}}
The following code prints student data and the index of each data:
enum Student {id,name,gender,department}void main() {// printing values from Student classprint(Student.values);//printing each index of valueStudent.values.forEach((v) =>print('value: $v, index: ${v.index}'));}