Collections in Dart
Introduction
Dart supports four types of collection with full-featured API. List, Set, Queue, Map are the four types of collection in Dart programming language. List, Set, Queue are iterable while Maps are not. Iterable collections can be changed i.e. their items can be modified, add, remove, can be accessed sequentially. The map doesn’t extend iterable.
Iterable
Iterable is an extract class, it can’t de instantiate directly.
An Iterable of string and int
void main() {
Iterable<int> var1 = [1,2,3,4];
Iterable<String> var2 = ['a','b','c','d'];
}
The difference with List and Iterable is that that element in the list can be accessed with their index value using []
operator while in Iterable value can’t be accessed using []
operator.
void main() {
Iterable<int> var1 = [1,2,3,4];
Iterable<String> var2 = ['a','b','c','d'];
print(var1[1]);
}
If we run the above program. We get the operator error.
Error compiling to JavaScript:
main.dart:4:13:
Error: The operator '[]' isn't defined for the class 'Iterable<int>'.
- 'Iterable' is from 'dart:core'.
print(var1[1]);
^
Error: Compilation failed.
Correct program:
void main() {
Iterable<int> var1 = [1,2,3,4];
Iterable<String> var2 = ['a','b','c','d'];
print(var1.elementAt(1));
}
Output: 2
Using for loop to print element of iterable
void main() {
Iterable<int> var1 = [1, 2, 3, 4];
for (var element in var1) {
print(element);
}
}
Performing few operations:
void main() {
Iterable<int> var1 = [1, 2, 3, 4];
print(var1.first);
print(var1.last);
print(var1.length);
print(var1.contains(1));
print(var1.skip(1));
print(var1.single);
}
OutPut:
1
4
4
true
(2, 3, 4)
Uncaught Error: Bad state: Too many elements
first
return the first element, last
return the last element, length
return the length of iterable, contains(1)
return the element at position 1, skip(1)
skip the element at position 1, single
return the element if iterable has only one element. Throw this error if it has more than one element or empty Uncaught Error: Bad state: Too many elements
.
List
A list is an array of elements arranged in an ordered sequence.
There are two types of List:
- Fixed-Length List
- Growable List
Fixed-Length List is a list that can’t be changed once initialized whereas the Growable list is dynamic in nature.
Fixed-length List
- Creating a Fixed-length List
void main() {
List<String> list = List(5);
}
The indexing value start with 0
and end with listOfLength-1
. So for the list
index values will be 0
to 4
.
- Here the
list
is empty, so let’s assign the value for each index:
void main() {
List<String> list = List(5);
list[0] = 'a';
list[1] = 'b';
list[2] = 'c';
list[3] = 'd';
list[4] = 'e';
}
- Printing the values:
print(list[1]);
- Updating the value:
void main() {
List<String> list = List(5);
list[0] = 'a';
list[1] = 'b';
list[2] = 'c';
list[3] = 'd';
list[4] = 'e';
list[0] = 'm';
print(list[0]);
}
Output:m
.
Growable List
- Growable List example:
void main() {
List<String> list = List();
}
- Inserting elements in List:
void main() {
List<String> list = List();
list.add('a');
list.add('b');
list.add('c');
}
- Updating element:
void main() {
List<String> list = List();
list.add('a');
list.add('b');
list.add('c');
list[1]= 'm';
}
Set
A set is an unordered collection of values. We can’t get the values by their index values as they are unordered. Values in set are unique i.e. they can’t be repeated.
- Creating a Set using a constructor:
void main() {
Set<int> set = Set();
}
- Creating a Set using List
void main() {
List<int> list = [1, 2, 3, 4];
Set<int> set = Set.from(list);
}
- Inserting elements in Set
void main() {
List<int> list = [1, 2, 3, 4];
Set<int> set = Set.from(list);
set.add(5);
set.add(6);
}
Maps
Maps is an unordered pair of key and values. The keys of the map must unique, values can be the same. Map is also called dictionary
or hash
.The size of a map is not fixed, we can add, delete edit the values of the map. The size of the map depends upon the number of elements in the map. The values in the map can only be accessed through the key name, not by the index values.
- Creating a map using constructor:
void main() {
Map<String, int> map = Map();
}
Here we have created a map named map
whose key type is String
and value type is int
.
- Adding a value and printing the map:
void main() {
Map<String, int> map = Map();
map['number'] = 1;
print(map);
}
Output:{number: 1}
Accessing value
by its key : print(map[‘number’]);
.
Printing all keys and values of the map:
void main() {
Map<String, int> map = Map();
map['number1'] = 1;
map['number2'] = 2;
map['number3'] = 3;
for (String keys in map.keys) {
print(keys);
}
for (int values in map.values) {
print(values);
}
}
Output:
number1
number2
number3
1
2
3
Printing Key-Value pair:
map.forEach((key, value) {
print("$key:$value");
});
OutPut:
number1:1
number2:2
number3:3
Thanks for reading this article ❤
If I got something wrong? Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.
If we got something wrong? Let me know in the comments. we would love to improve.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter related queries.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.