Dart: Enumeration

enum enum_name {  
   enumeration list 
}
  • The enum_name specifies the enumeration type name
  • The enumeration list is a comma-separated list of identifiers

Example

enum Status {  
   dog, 
   cat, 
   chicken, 
   duck
}  
void main() { 
   print(Status.values); 
   Status.values.forEach((v) => print('value: $v, index: ${v.index}'));
   print('cat: ${Status.cat}, ${Status.cat.index}'); 
   print('cat index: ${Status.values[1]}'); 
}

=> output

[Status.none, Status.running, Status.stopped, Status.paused] 
value: Status.dog, index: 0 
value: Status.cat, index: 1 
value: Status.chicken, index: 2 
value: Status.duck, index: 3 
cat: Status.cat, 1 
cat index: Status.cat

Leave a Reply

Your email address will not be published. Required fields are marked *