Dart Programming Tutorial on 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}