Updated 15 November 2023
Dart, unlike other programming languages, doesn’t support arrays. Collections in Dart are similar to arrays. The dart:core library and in other words classes enable Collection support in Dart scripts.
We are going to learn about Collections in this blog.
Read more about Flutter app development services by Mobikul.
A list is the collection of the ordered group of collections. The dart::core library offers the list class that allows us to create and modify the list as similarly to the array. It provides the following types of lists.
Fixed Length List – We cannot change the list’s length at runtime.
Growable List – We can change the length of the list at run-time.
Given below is an example of Dart implementation of List.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
void main() { List logTypes = new List(); logTypes.add("WARNING"); logTypes.add("ERROR"); logTypes.add("INFO"); // iterating across list for(String type in logTypes){ print(type); } // printing size of the list print(logTypes.length); logTypes.remove("WARNING"); print("size after removing."); print(logTypes.length); } //OUTPUT: WARNING ERROR INFO 3 size after removing. 2 |
An object collection in which every item may be stated simultaneously is called a set. The Set class has access to the dart::core library’s resources.
There are two ways:
Identifier = new Set()
Identifier = new Set.from(Iterable)
Where, Iterable represents a list of values to add to a Set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
void main() { Set numberSet = new Set(); numberSet.add(100); numberSet.add(20); numberSet.add(5); numberSet.add(60); numberSet.add(70); print("Default implementation :${numberSet.runtimeType}"); for(var no in numberSet) { print(no); } } //OUTPUT: 100 20 5 60 70 |
The key-value pair data is gathered in the maps. A unique key is used to store each value. Any kind of value and key may be used in the dart similarly to other programing languages. A dynamic collection is called a map. We can state that run-time modifications can be made to a map. The Map class is made accessible to work with it by the dart::core library.
We can declare Map in two ways:
We can declare the map using the map literals as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
void main() { // Creating Map using is literals var web = {'position1' : 'webkul', 'position2' : 'software', 'position3' : 'company'}; // Printing Its content print(web); // Printing Specific Content // Key is defined print(web['position1']); // Key is not defined print(web[0]); } //OUTPUT: {position1: webkul, position2: software, position3: company} webkul null |
We can declare the map using the map constructors as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
void main() { // Creating Map using Constructors var webkul = new Map(); // Inserting values into Map webkul ["position1"] = 'webkul'; webkul ["position2"] = 'software'; webkul ["position3"] = 'company'; // Printing Its content print(webkul); // Printing Specific Content // Key is defined print(webkul["position1"]); } //OUTPUT: {position1: webkul, position2: software, position3: company} webkul |
A queue is a group of items that are stored in a first-in, first-out manner. It is manipulable from both ends. In other words, we can add the element from one end and remove it from the other.
We can declare a Queue in two ways:
Let’s look at the syntax for building a Dart queue and adding components to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//Syntax: Queue var_name = new Queue(); //Input: import 'dart:collection'; void main() { Queue<String> codeStudio = new Queue<String>(); // Adding elements codeStudio.add("Code"); codeStudio.add("Studio"); codeStudio.add("Blog"); // Printing all the inserted elements print(codeStudio); } //OUTPUT: {Code, Studio, Blog} |
Let’s now examine the syntax for building a queue in Dart using an existing list and adding members to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
//Syntax: Queue<E> var_name = new Queue<E>.from(list_name); // With type notation(E) var var_name = new Queue.from(list_name); // Without type notation //Input: import 'dart:collection'; void main() { // Creating List List<String> my_list = ["Code","Studio","Blog"]; // Creating a Queue (my_queue) through a List (my_list) Queue<String> my_queue = new Queue<String>.from(my_list); // Printing elements of the queue my_queue print(my_queue); } //OUTPUT: {Code, Studio, Blog} |
The table below includes a list of numerous key functions along with an example of how to utilise each one.
In this article, we have extensively discussed some common Collection methods in Dart. We hope that this blog has helped you enhance your knowledge regarding Dart Collection methods, and to learn more, check out our other article on Dart Lists.
You can also check other blogs from here for more knowledge click here.
For more understanding please can go through this Link.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.