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, Dart collections are of a different type. In other words, a single Dart collection can hold values for several types of data. However, the Dart collection can also be retained for the same values or values of the same type.
Dart Generics provides a data enforcement tool for the type of data that can be stored by the collection. These collections can be called type-safe collections.Type safety is a unique feature of the Dart programming, which ensures that the memory block can only contain data for a specific type of data.
Generations are a way to support type - safety implementation in all Dart collections. A pair of angular brackets is used to declare a type-safe collection. Angular brackets include types of collection data. The syntax to declare or implement generics is as follows :
Syntax -
Collection_name < data_type > identifier = new Collection_name < data_type >
We can make safe use of a variety of Dart items such as list, queue, map, and Set. It is also supported by all the uses of the above define collection types. Let us consider the following example where we create a generic list.
Example - Generics List
// creating a generic list
void main( ) {
// declaring a new generic list
List < String > list_Str = new List < String >( ) ;
// adding the items of type String to the list
list_Str.add( " This " ) ;
list_Str.add( " is " ) ;
list_Str.add( " a " ) ;
list_Str.add( " String " ) ;
// Iterating over the list to print the items of the Generic List
for ( String i in list_Str ) {
print( i ) ;
}
}
Output :
This
is
a
String
Explanation :
We created a list containing the string type-safe and used the add element feature using the add( ) function.
If we try to add a value other than the value of a specified it will happen with a merge error. Let's understand the following example -
Example - 2
void main( ) {
// declaring a generic list
List < String > list_Str = new List < String >( ) ;
// adding an integer value in the list
list_Str.add( 120 ) ;
// adding a String value in the list
list_Str.add( " Generic " ) ;
list_Str.add( " List " ) ;
// Iterating over the list to print the items of the Generic List
for ( String i in logTypes ) {
print( i ) ;
}
}
Output :
generics.dart : 3:17 : Error : The argument type ' int ' can't be assigned to the parameter type ' String '.
list_Str.add( 120 ) ;
Let's understand another example -
Example - Generic Set
Let us consider the following example that depicts generic Set :
void main( ) {
// declaring a generic set
Set < int > num_list = new Set < int >( ) ;
// adding integer values in the set
num_set.add( 1 ) ;
num_set.add( 2 ) ;
num_set.add( 3 ) ;
num_set.add( 4 ) ;
num_set.add( 5 ) ;
for( var i in num_set ) {
print( i ) ;
}
}
Output :
1
2
3
4
5
Example - Generics Queue
Let us consider the following example that depicts generic queue :
import ' dart : collection ' ;
void main( ) {
// declaring a generic queue
Queue< int > queue = new Queue < int > ( ) ;
print( " Default implementation of ${ queue.runtimeType } " ) ;
// adding the integer values in the queue at the rear end
queue.addLast( 5 ) ;
queue.addLast( 1 ) ;
queue.addLast( 2 ) ;
queue.addLast( 0 ) ;
queue.addLast( 2 ) ;
// removing the first element of queue that is 5
queue.removeFirst( ) ;
for( int i in queue ) {
print( i ) ;
}
}
Output :
Default implementation of ListQueue< int >
1
2
0
2
Generic Map
As we know that announcing a map requires a key and a value.
The syntax is provided below.
Syntax :
< Key_type, value_type >
Example -
void main( ) {
Map < String, String > m = { ' Name ' : ' Yukta ' , ' Rollno ' : ' 157 ' } ;
print( ' Map : ${ m } ' ) ;
}
Output :
Map : { Name : Yukta , Rollno : 157 }