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:

  1. The enum keyword creates an enumerated data type.
  2. enum_name defines the name of the enumerated type.
  3. Commas must be used to separate the data members within the enumerated class.
  4. 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 enum
enum Shot {
// data members
Dart,
Enumeration,
types
}
void main() {
Shot educativeShot;
// Print the value in Shot class
for (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 class
print(Student.values);
//printing each index of value
Student.values.forEach((v) =>
print('value: $v, index: ${v.index}'));
}

Free Resources