×

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

Dart Installation Guide

There are various ways of compiling and running an application created in Dart, either by compiling the Dart code to JavaScript using the Dart2js tool or by running on the...

3 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 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.

Golang vs Dart

Go is the procedural programming language. It was founded in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson at Google but was launched in 2009 as the language of...

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.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

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 Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

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 Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart HTML DOM

All web pages can be viewed as objects and exist within the browser window. We can access a web page using a web browser and it needs to be connected...

3 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.

Dart URIs

The Uri class supports encoding and decoding of strings with the help of functions to be used in URIs ( which may also be known as URLs ). These functions...

6 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Constants

Dart Constants are the objects or variables whose values can’t change or modify during the execution of the program. Their use case is when we want a particular value to...

2 minutes read.

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 If-else statement

In the previous section, we studied about if statements in detail. If – block is executed when the condition evaluates to true, else if the condition evaluates to false, then...

1 minute read.

Dart Tutorial

Dart is an open-source, structured programming language developed by Google. It is a high-level programming language that emerged in 2011, but its stable version emerged in 2017. It is largely used...

4 minutes read.

Dart Type Test Operators

Type Test Operators in Dart These operators are used to check the types of expressions at runtime. Dart is a typed language, and we often want to assert that a value...

1 minute read.

Dart lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.