30% Therapy – 40% Practice – 30% Work project

Dart Programming – Generics



Dart is an optionally typed language. Collections in Dart are heterogeneous by default. In other words, a single Dart collection can host values of various types. However, a Dart collection can be made to hold homogenous values. The concept of Generics can be used to achieve the same.

The use of Generics enforces a restriction on the data type of the values that can be contained by the collection. Such collections are termed as type-safe collections. Type safety is a programming feature which ensures that a memory block can only contain data of a specific data type.

All Dart collections support type-safety implementation via generics. A pair of angular brackets containing the data type is used to declare a type-safe collection. The syntax for declaring a type-safe collection is as given below.

Syntax

Collection_name <data_type> identifier= new Collection_name<data_type> 

The type-safe implementations of List, Map, Set and Queue is given below. This feature is also supported by all implementations of the above-mentioned collection types.

Example: Generic List

void main() { 
   List <String> logTypes = new List <String>(); 
   logTypes.add("WARNING"); 
   logTypes.add("ERROR"); 
   logTypes.add("INFO");  
   
   // iterating across list 
   for (String type in logTypes) { 
      print(type); 
   } 
}

It should produce the following output

WARNING 
ERROR 
INFO

An attempt to insert a value other than the specified type will result in a compilation error. The following example illustrates this.

Example

void main() { 
   List <String> logTypes = new List <String>(); 
   logTypes.add(1); 
   logTypes.add("ERROR"); 
   logTypes.add("INFO"); 
  
   //iterating across list 
   for (String type in logTypes) { 
      print(type); 
   } 
} 

It should produce the following output

1                                                                                     
ERROR                                                                             
INFO

Example: Generic Set

void main() { 
   Set <int>numberSet = new  Set<int>(); 
   numberSet.add(100); 
   numberSet.add(20); 
   numberSet.add(5); 
   numberSet.add(60);
   numberSet.add(70); 
   
   // numberSet.add("Tom"); 
   compilation error; 
   print("Default implementation  :${numberSet.runtimeType}");  
   
   for(var no in numberSet) { 
      print(no); 
   } 
} 

It should produce the following output

Default implementation :_CompactLinkedHashSet<int> 
100 
20 
5 
60 
70

Example: Generic Queue

import ''dart:collection 
void main() { 
   Queue<int> queue = new Queue<int>(); 
   print("Default implementation ${queue.runtimeType}");  
   queue.addLast(10); 
   queue.addLast(20); 
   queue.addLast(30); 
   queue.addLast(40); 
   queue.removeFirst();  
   
   for(int no in queue){ 
      print(no); 
   } 
}

It should produce the following output

Default implementation ListQueue<int> 
20 
30 
40

Generic Map

A type-safe map declaration specifies the data types of −

  • The key
  • The value

Syntax

Map <Key_type, value_type>

Example

void main() { 
   Map <String,String>m={''name'':''Tom'',''Id'':''E1001''}; 
   print(''Map :${m}''); 
} 

It should produce the following output

Map :{name: Tom, Id: E1001}
Translate »