×

Dart Enumerations

Enumeration data type in Dart

In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword. 

Implementation = 

enum enum_name 

{

         enumeration

.

.

list

}
  1. enum_name refers to the name of the enumeration. 
  2. The list is enclosed within the curly braces ‘ { } ’.
  3. The enumeration list mentioned here is the list of constant values separated by commas. Every value in the enum is associated with an integer value. The first value of the list corresponds to index ‘ 0 ’, and every value succeeds the other by +1. 
  4. Note that the last member of the enumeration list doesn’t end with a comma or semi-colon. 

For example, 

enum prog_languages

{

         Dart,

         Flutter,

         Java,

         Python

}

Program

enum prog_language

{   

  Dart ,

  Flutter ,

  Java ,

  JavaScript ,

  Python ,

  PHP ,

  C ,

  Go ,

  SQL ,

  MongoDb

} 

void main( )

{ 

   print( " Here is the list of Proramming Language Enumeration " ) ; 

   print( prog_language.values ) ; 

prog_language.values.forEach( ( i ) => print( ' Value : $i, Index : ${ i.index } ' ) ) ;

   print( prog_language.values[ 5 ] ) ;

}

Output

Here is the list of Proramming Language Enumeration

[prog_language.Dart, prog_language.Flutter, prog_language.Java, prog_language.JavaScript, prog_language.Python, prog_language.PHP, prog_language.C, prog_language.Go, prog_language.SQL, prog_language.MongoDb]

Value : prog_language.Dart, Index : 0

Value : prog_language.Flutter, Index : 1

Value : prog_language.Java, Index : 2

Value : prog_language.JavaScript, Index : 3

Value : prog_language.Python, Index : 4

Value : prog_language.PHP, Index : 5

Value : prog_language.C, Index : 6

Value : prog_language.Go, Index : 7

Value : prog_language.SQL, Index : 8

Value : prog_language.MongoDb, Index : 9

prog_language.PHP

Program

enum college_semester

{   

   first ,

   second ,

   third ,

   fourth ,

   fifth ,

   sixth ,

   seventh ,

   eighth

}   

void main( )

{   

   print( college_semester.values ) ;

   print(" \n " ) ;

college_semester.values.forEach( ( v ) => print( ' value : $v, index : ${ v.index } ' ) ) ; 

   print( " \n " ) ;

   print( ' You are in : ${ college_semester.seventh } ' ) ; 

   print(  " \n " );

   print( ' College semester 3rd corresponds to index : ${ college_semester.seventh.index } ' ) ;

}

Output :

[college_semester.first, college_semester.second, college_semester.third, college_semester.fourth, college_semester.fifth, college_semester.sixth, college_semester.seventh, college_semester.eigth]

  n

value : college_semester.first, index : 0

value : college_semester.second, index : 1

value : college_semester.third, index : 2

value : college_semester.fourth, index : 3

value : college_semester.fifth, index : 4

value : college_semester.sixth, index : 5

value : college_semester.seventh, index : 6

value : college_semester.eigth, index : 7

You are in : college_semester.seventh

College semester 3rd corresponds to index : 6

Note : If one tries to print the value against an index that doesn’t exist in the enumeration list, the compiler throws an error.

For example, 

enum prog_language

{   

  Dart ,

  Flutter ,

  Java ,

  JavaScript ,

  Python ,

  PHP ,

  C ,

  Go ,

  SQL ,

  MongoDb

} 

void main( )

{ 

   print( prog_language.values[ 5 ] ) ;

   print( prog_language.values[ 12 ] ) ;

}

Output :

Dart Enumerations

Drawbacks of enumerations:

  1. The values in an enumeration cannot be instantiated separately.
  2. Enumerations cannot be subclassed.

Related Topics

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Exception Handling

Exception Class – An exception is any runtime error that leads to abrupt termination of the program. It helps in addressing the error programmatically. It is aimed to be caught...

4 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Dart Enumerations

Enumeration data type in Dart In simple terms, Enumerations are referred to as named constant values. Constant values are declared as enumerations using the ‘enum’ keyword.  Implementation =  enum enum_name {    ...

2 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.

Dart Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Dart Logical Operators

Logical operators are the operators that combine two or more conditions, and accordingly return a Boolean value : true or false. Assume the value of variable A is 25 and B...

2 minutes read.

Dart Syntax

In the previous tutorial, we coded our first program in Dart on various platforms understanding the basic functionality of each line of code.  Let us understand the syntax for Dart language...

4 minutes read.

Typedef in Dart

In Dart, typedef is used to generate a function type that we can use as a type annotation for declaring variables and return types of the function type. An alias...

4 minutes read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.

Dart Important Concepts

1. Anything placed in a variable is an object, and an object is an instance of a class. Numbers, functions, and null are all objects in Dart, and all these...

3 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart Generators

Synchronous Generator Dart Generator is a unique function that allows us to generate price sequences. Generators return values when needed; means that the value is generated if we try to duplicate...

5 minutes read.

Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.