Open In App

Data Enumeration in Dart

Last Updated : 11 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Enumerated types (also known as enumerations or enums) are primarily used to define named constant values. The enum keyword is used to define an enumeration type in Dart. The use case of enumeration is to store finite data members under the same type definition. 

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

Let’s analyze the above syntax:

  1. The enum is the keyword used to initialize enumerated data type.
  2. The variable_name as the name suggests is used for naming the enumerated class. 
  3. The data members inside the enumerated class must be separated by the commas.
  4. Each data member is assigned an integer greater than then the previous one, starting with 0 (by default).
  5. Make sure not to use semi-colon or comma at the end of the last data member.

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

Dart




// 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);
  }
}


Output:   

Gfg.Welcome
Gfg.to
Gfg.GeeksForGeeks

Note: Notice in the above example the strings are not enclosed with quotes, so that it can be used to print different results by comparing them with the values inside the enum. 

Example 2: Using switch-case to print result. 

Dart




enum Gfg {
  Welcome, to, GeeksForGeeks
}
 
void main() { 
   
  // Assign 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;
  }
}


Output:  

This is the correct case. 

Note: The enumerated class does not hold all types of data, rather it stores only string values without the quotes over them.
  
Limitation of Enumerated Data Type:   

  1. It cannot be subclassed or mixed in.
  2. It is not possible to explicitly instantiate an enum.
     


Previous Article
Next Article

Similar Reads

Dart - Data Types
Like other languages (C, C++, Java), whenever a variable is created, each variable has an associated data type. In Dart language, there are the types of values that can be represented and manipulated in a programming language. In this article, we will learn about Dart Programming Language Data Types. Data Types in DartThe data type classification i
4 min read
How to Combine Lists in Dart?
In Dart programming, the List data type is similar to arrays in other programming languages. A list is used to represent a collection of objects. It is an ordered group of objects. The core libraries in Dart are responsible for the existence of List class, its creation, and manipulation. There are 5 ways to combine two or more list: Using addAll()
3 min read
Dart - Super and This keyword
Super Keyword in Dart: In Dart, super keyword is used to refer immediate parent class object. It is used to call properties and methods of the superclass. It does not call the method, whereas when we create an instance of subclass than that of the parent class is created implicitly so super keyword calls that instance. Advantages of super keyword:
3 min read
Dart - Standard Input Output
Standard Input in Dart: In Dart programming language, you can take standard input from the user through the console via the use of .readLineSync() function. To take input from the console you need to import a library, named dart:io from libraries of Dart. About Stdin Class: This class allows the user to read data from standard input in both synchro
2 min read
Dart - Unit Testing
Unit Testing is a process of testing individual components of a software/application separately, to ensure that each component is functioning as intended. It provides multiple advantages such as - Ability to test out individual components without running the whole software/application.Pinpoint errors easily inside a specific component. Dart provide
3 min read
Generators in Dart
Generators in Dart allows the user to produce a sequence of value easily. One can generate a sequence of values in dart with the help of two generator functions : Synchronous Generator: Returns an Iterable object.Asynchronous Generator: Returns a Stream object.Synchronous Generator in DartThe synchronous generator returns an iterable object i.e. it
2 min read
Dart - Late Keyword
In Flutter, the late keyword is used to indicate that a non-nullable variable will be initialized later in the code. if the non-nullable variable does not initialize before it using, it raised an error. For Example Let's create a non-nullable variable _title of type String and make it late. late String _title; Now if we do not initialize this varia
4 min read
Dart - Loops
A looping statement in Dart or any other programming language is used to repeat a particular set of commands until certain conditions are not completed. There are different ways to do so. They are: for loopfor... in loopfor each loopwhile loopdo-while loop for loop For loop in Dart is similar to that in Java and also the flow of execution is the sa
2 min read
Switch Case in Dart
In Dart, switch-case statements are a simplified version of the nested if-else statements. Its approach is the same as that in Java. Syntax: switch ( expression ) { case value1: { // Body of value1 } break; case value2: { //Body of value2 } break; . . . default: { //Body of default case } break; } The default case is the case whose body is executed
2 min read
Dart - Loop Control Statements (Break and Continue)
Dart supports two types of loop control statements: Break Statement Continue Statement Break Statement: This statement is used to break the flow of control of the loop i.e if it is used within a loop then it will terminate the loop whenever encountered. It will bring the flow of control out of the nearest loop. Syntax: break; Example 1: Using break
4 min read
Article Tags :