Dart Generics

 

Dart Generics

Dart Generics are the same as the Dart collections, which are used to store the homogenous data. As we discussed in the Dart features, it is an optionally typed language.

By default, Dart Collections are the heterogeneous type. In other words, a single Dart collection can hold the values of several data types. However, a Dart collection can be also stored the homogenous values or same type values.

The Dart Generics provides the facility to enforce a limit on the data type of the values that can be stored by the collection. These collections can be referred to as the type-safe collections.

Type safety is a unique feature of the Dart programming, which makes sure that a memory block can only contain the data of a specific data type.

The generics are a way to support type-safety implementation for all Dart collections. The pair of the angular bracket is used to declare the type-safe collection. The angular bracket consists of the data-types of the collection. The syntax is given below.

Syntax -

  1. Collection_name <data_type> identifier = new Collection_name<data_type>   

We can do the type-safe implementation of various Dart objects such as List, Queue, Map, and Set. It is also supported by all implementation of the above define collection types. The syntax is given below.

Example - Generics List

  1. void main() {   
  2.    List <String> logStr = new List <String>();   
  3.    logStr.add("CHECK");   
  4.    logStr.add("ERROR");   
  5.    logStr.add("INFO");   
  6.     
  7.    //iterating across list   
  8.    for (String i in logStr) {   
  9.       print(i);   
  10.    }   
  11. }  

Output

CHECK
ERROR
INFO

Explanation:

We created a list that holds the string type-safe and used the add element into it by using add() function.

If we try to insert the other than the specified value then it will through a compilation error. Let's understand the following example -

Example - 2

  1. void main() {   
  2.    List <String> logStr = new List <String>();   
  3.    logStr.add(511);   // Add integer value  
  4.    logStr.add("ERROR");   
  5.    logStr.add("INFO");   
  6.     
  7.    //iterating across list   
  8.    for (String i in logTypes) {   
  9.       print(i);   
  10.    }   
  11. }  

Output

generics.dart:3:17: Error: The argument type 'int' can't be assigned to the parameter type 'String'.
logTypes.add(511);

Let's understand another example -

Example - Generic Set

  1. void main() {   
  2.    Set <int>numberSet = new  Set<int>();   
  3.    numberSet.add(10);   
  4.    numberSet.add(20);   
  5.    numberSet.add(30);   
  6.    numberSet.add(40);  
  7.    numberSet.add(50);   
  8.      
  9.    // numberSet.add("");   
  10.   // compilation error;   
  11.    print("Default implementation  :${numberSet.runtimeType}");    
  12.      
  13.    for(var i in numberSet) {   
  14.       print(i);   
  15.    }   
  16. }  

Output

10
20
30
40
50

Example - Generics Queue

  1. import 'dart:collection';   
  2. void main() {   
  3.    Queue<int> queue = new Queue<int>();   
  4.    print("Default implementation ${queue.runtimeType}");    
  5.    queue.addLast(100);   
  6.    queue.addLast(205);   
  7.    queue.addLast(315);   
  8.    queue.addLast(470);   
  9.    // Remove the first element of queue   
  10.    queue.removeFirst();    
  11.      
  12.    for(int i in queue){   
  13.       print(i);   
  14.    }   
  15. }  

Output

Default implementation ListQueue<int>
205
315
470

Generic Map

As we know that declaring map require the key and value. The syntax is given below.

Syntax:

  1. Map <Key_type, value_type>  

Example -

  1. void main() {   
  2.    Map <String, String>m={'name':'Joseph','Rollno':'Std1001'};   
  3.    print('Map :${m}');   
  4. }  

Output

Map :{name: Joseph, Rollno: Std1001}

Comments