- Dart Programming - HTML DOM
- Dart Programming - Unit Testing
- Dart Programming - Concurrency
- Dart Programming - Async
- Dart Programming - Libraries
- Dart Programming - Typedef
- Dart Programming - Debugging
- Dart Programming - Exceptions
- Dart Programming - Packages
- Dart Programming - Generics
- Dart Programming - Collection
- Dart Programming - Object
- Dart Programming - Classes
- Dart Programming - Interfaces
- Dart Programming - Functions
- Dart Programming - Enumeration
- Dart Programming - Runes
- Dart Programming - Symbol
- Dart Programming - Map
- Dart Programming - Lists
- Dart Programming - Lists
- Dart Programming - Boolean
- Dart Programming - String
- Dart Programming - Numbers
- Dart Programming - Decision Making
- Dart Programming - Loops
- Dart Programming - Operators
- Dart Programming - Variables
- Dart Programming - Data Types
- Dart Programming - Syntax
- Dart Programming - Environment
- Dart Programming - Overview
- Dart Programming - Home
Dart Programming Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 pst 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 pst 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}Advertisements