Dart Tutorial

Dart Tutorial Single-Page Application Architecture Dart Features Dart Installation Guide Dart Basic Program Dart Syntax Dart Keywords Dart Variables Dart Comments Dart Standard Input Output Dart Important Concepts

Data Types

Built-in Data Types Numbers Strings Booleans Lists Sets Maps Runes and Graphemes Symbols Enumerations Constants Queues

Other data types

Objects Future and stream Iterable Miscellaneous types

OPERATORS

Precedence and associativity Arithmetic operators Equality and Relational operators Type Test Operators Assignment Operators Logical Operators Bitwise and Shift Operators Miscellaneous operators

Control Flow Statements

Introduction If statement If-else statement If-else-if statement Loops Switch and case Dart Break And Continue Assert In Dart

FUNCTIONS

Dart function Types of Functions Anonymous function main( ) function Lexical scope and closure Recursion Common Collection Methods

Object Oriented Concepts

Dart Object-Oriented Concepts Dart Classes Dart Constructors Dart This Keyword Dart Super Keyword Static Members Method Overriding Dart Interfaces Inheritance Dart Abstract Classes Dart Builder Classes Dart Callable Classes

Dart Type System

Dart Type System Dart Soundness Dart Type Inference

MISCELLANEOUS

Dart Isolates Dart Typedef Dart Metadata Dart Packages Dart Generics Dart Generators Dart Concurrency Dart Unit Testing Dart Html Dom Dart URIs Dart Extends, With and Implements Keywords Dart Optional Parameters Rust Vs Dart C++ vs Dart Golang Vs Dart Dart Basics Exception Handling

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.