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.
enum enum_name{
// data members
member1,
member2,
....,
memberN
}
where:
enum
keyword creates an enumerated data type.enum_name
defines the name of the enumerated type.0
. (by default).Note: In the conclusion of the last data member, avoid using a semicolon or comma.
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}'));}