Data Enumeration in Dart

Enumerated types (also known as enumerations or enums) are a special kind of class used to represent a predefined fixed number of constant values. They are declared by the use of enum keyword. This type of data class is very useful in the case where we have a finite number of data members, and we want to store them under the same datatype.

enum variable_name{
  // Insert the data members as shown
  member1, member2, member3, ...., memberN
}

Here,

  1. enum is the keyword used to initialize enumerated data.
  2. variable_name is the variable name of the enumerated data class.
  3. The data members are separated by the commas.
  4. Each data member is assigned an integer greater than then the previous one, starting with 0 (by default).

Note: It must be noted that there is no semi-colon or comma at the end of the last data member.
 

Example 1: Printing all the elements from the enum data class. 
 

Dart



filter_none

edit
close

play_arrow

link
brightness_4
code

// dart program to print all the 
// elements from the enum data class
  
// Creating enum with name Gfg
enum Gfg {
    
  // Inserting data
  Welcome, to, GeeksForGeeks
}
  
void main() { 
    
  // Printing the value
  // present in the Gfg
  for (Gfg geek in Gfg.values) {
    print(geek);
  }
}

chevron_right


 

Output:  

Gfg.Welcome
Gfg.to
Gfg.GeeksForGeeks

Note: It must be noted that in the above example the string is not enclosed with quotes. You can print different results by comparing them with the values inside the enum. 
 

Example 2: Using switch-case to print result.

Dart

filter_none

edit
close

play_arrow

link
brightness_4
code

enum Gfg {
  Welcome, to, GeeksForGeeks
}
  
void main() {  
    
  // Assing a value from
  // enum to a variable geek
  var geek = Gfg.GeeksForGeeks;
    
  // Switch-case
  switch(geek) {
    case Gfg.Welcome: print("This is not the correct case."); 
    break;
    case Gfg.to: print("This is not the correct case."); 
    break;
    case Gfg.GeeksForGeeks: print("This is the correct case."); 
    break;
  }
}

chevron_right


 

Output: 

This is the correct case. 

Note: You can’t just store all the types of values inside enum, it takes an only string value, and the String is not meant to be stored within quotes, if you do so then it will show an error.
 Limitation of Enumerated data type:  

  1. You can’t subclass, mix in, or implement an enum.
  2. You can’t explicitly instantiate an enum.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.