×

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 }

Related Topics

Dart Optional Parameters

Dart functions can have optional parameters with default values. When we create a function, we can specify parameters that calling code can provide; but if the calling code chooses not...

2 minutes read.

Dart Strings

Strings data type in Dart A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’)...

3 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

Dart Variables

Variables are used to store the value of a particular data type referred to in the whole program by a name or identifier. They refer to a memory location as...

5 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart Assignment Operator

Assignment operators are the operators that assign value to the variables. The value on the right-hand side is assigned to the variable on the left-hand side. We can also use...

4 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.

Dart Basics

Dart, as earlier mentioned multiple times, is very similar to C, C++ and Java. It is an object-oriented, garbage collecting and class-based programming language. Let’s look further and see what Dart has...

10 minutes read.

Dart Numbers

Dart provides an in-built data type ‘Numbers’ that provides two types of values: intdouble 1. int data type int data type supports integer values, and their size or values range is platform-dependent. Integers...

4 minutes read.

Rust vs Dart

Rust is a system-level programming language that stands next to C ++ in terms of syntax, but offers greater speed and memory security. Dart, on the other hand, is an...

2 minutes read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.