Table of contents
1.
Introduction
2.
Dart Generics
2.1.
Generic List
2.2.
Generic Map
2.3.
Generic Set
2.4.
Generic Queue
3.
Frequently Asked Questions
3.1.
What will happen if you add elements of a different data type to a list?
3.2.
What is type-safety in programming?
3.3.
What is the purpose of the addLast and addFirst functions in a queue?
4.
Conclusion 
Last Updated: Mar 27, 2024

Dart Generics

Author Pradeep Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will look into the Dart Generics. All the collections in Dart are, by default, of heterogeneous nature. We will discuss how we can use generics to construct collections that can hold homogeneous values. Before moving forward with this blog, I suggest you check out our other blog on Dart Collections, which will help you understand the concept of Generics better.

Dart Generics

Collections in Dart are heterogeneous by default, i.e., a single collection can hold values of different data types. A Dart collection, on the other hand, can be made to store values that are all the same. Generics can be used to accomplish the same goal. 

When Generics are used, the data type of the values held within the collection is restricted. Type-safe collections are just that. A type-safe collection is declared with a pair of angle brackets enclosing the data type. The following is the syntax for the same:

Syntax:

CollectionName <dataType> identifier= new CollectionName<dataType>

In the further sections, we will see how type safety can be accomplished in collections like List, Map, Set, and Queue.

Generic List

A List is essentially an ordered group of objects. In the example below, we will see how a list of integer data type can be created.

void main() {
  // Creating a list of integers
  List<int> squares = [];
  // Adding elements in the list
  squares.add(1);
  squares.add(4);
  squares.add(9);
  squares.add(16);
  squares.add(25);

  // Printing the list
  print(squares);
}

Output:

[1, 4, 9, 16, 25]

Generic Map

A map consists of a set of key-value pairs. Every key has a corresponding value associated with it. The datatype of key and value doesn't have to be the same. For example,

void main() {
  // Creating a set of integers
  Map<int, String> mp = {};
  // Adding elements in the map
  mp[1] = "Red";
  mp[2] = "Blue";
  mp[3] = "Black";
  // Printing the Map
  print(mp);
}

Output:

{1: Red, 2: Blue, 3: Black}

Here, the map mp is a mapping from integer to Strings. It holds integer keys and colors corresponding to those keys.

Generic Set

A set is identical to a list, except that it only contains unique values. In case you add one element multiple times; only one value of the same will be stored in the set. For example,

void main() {
  // Creating a set of integers
  Set<int> st = {};
  // Adding elements in the Set
  st.add(1);
  st.add(2);
  st.add(3);
  st.add(1);
  // Printing the Set
  print(st);
}

Output:

{1, 2, 3}

Here, we have added element "1" twice in the set. But, as you can see from the output, the final set contains only one value of that element.

Generic Queue

A queue is a popular data structure used to insert data in FIFO(First In First Out) manner. Dart provides the functionality of adding elements from both ends in a queue. For example,

import 'dart:collection';

// Main function to run the program
void main() {
  Queue<String> colours = new Queue<String>();

  // Inserting elements from the front
  colours.addFirst("Red");
  colours.addFirst("Blue");

  // Inserting elements from the back
  colours.addLast("Green");
  colours.addLast("Black");

  // Inserting elements from the front
  colours.addFirst("Orange");
  colours.addFirst("Yellow");

  // Printing the queue
  print(colours);
}

Output:

{Yellow, Orange, Blue, Red, Green, Black}

Frequently Asked Questions

What will happen if you add elements of a different data type to a list?

Trying to do so will throw a compilation error.

 

What is type-safety in programming?

Type safety is a programming feature that assures that a memory block can only hold data of a specific data type.

 

What is the purpose of the addLast and addFirst functions in a queue?

addFirst function is used to insert elements from the front in a queue. In contrast, the addLast function is used to insert elements from the back.

Conclusion 

In this article, we have extensively discussed the Generics in Dart. We hope that this blog has helped you enhance your knowledge regarding the Dart Generics, and to learn more, check out our other article on Dart Abstract Classes. And also check out the awesome content on the Coding Ninjas Website,
Android DevelopmentCoding Ninjas Studio ProblemsCoding Ninjas Studio Interview BundleCoding Ninjas Studio Interview ExperiencesCoding Ninjas CoursesCoding Ninjas Studio Contests, and Coding Ninjas Studio Test SeriesDo upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass