In this chapter, we will learn about dart lists data types and various properties and methods available within lists with the help of examples.
Dart Lists
The lists are collections of ordered groups of objects. The lists are also known as arrays.
We can create a list data type by enclosing the values in square braces. For example,
var numbers = [4, 8, 5];
Another way to create a list is by passing the new keyword to the List object. For example,
var names = new List();
names[0] = 4;
names[1] = 8;
names[2] = 5;
In dart lists, all the values should have the same data type.
Accessing List Values
The list values are also known as elements.
Each element has it’s index number which indicates the position in the list.
The element’s index starts with 0. And the last element’s index will be length-1, where length indicates the number of elements in the list.
The index number can be used to access the element from the list. For example,
void main() {
var numbers = [4, 8, 5];
print(numbers[0]);
print(numbers[1]);
print(numbers[2]);
}
Output
4
8
5
Updating List Values
We can update the list values by accessing the index of the list and adding the new values. For example,
void main() {
var numbers = [4, 8, 5];
print(numbers);
numbers[2] = 22;
print(numbers);
}
Output
[4, 8, 5]
[4, 8, 22]
Dart List Properties
Dart list also provides very useful properties. Some of them are:
Properties | Description |
isEmpty | Returns true if the list has no elements |
isNotEmpty | Returns true if the list has at least one element |
length | Returns the number of elements in the list |
first | Returns the first element in the list |
last | Returns the last element in the list |
reversed | Returns an iterable object containing the lists values in the reverse order |
single | Checks if the list has only one element and returns it |
Dart List Properties Examples
void main() {
var names = ["John", "Jackson", "Anna", "Amanda"];
var names1 = ["Amanda"];
print(names.isEmpty);
print(names.isNotEmpty);
print(names.length);
print(names.first);
print(names.last);
print(names.reversed);
print(names1.single);
}
Output
false
true
4
John
Amanda
(Amanda, Anna, Jackson, John)
Amanda
Dart List Methods
Dart list also provides various useful methods. Some of them are:
Methods | Description |
add() | Adds the specified value at the end of the list |
addAll() | Adds multiple values to the given list |
insert() | Adds the specified value at the specified index of the list |
remove() | Removes one element at a time from the given list. |
removeAt() | Removes an element from the specified index position |
removeLast() | Remove the last element from the given list |
removeRange() | Removes the item within the specified range |
Dart List Methods Examples
void main() {
var numbers = [4, 8, 5];
print(numbers);
numbers.add(12);
print(numbers);
numbers.addAll([15, 16, 18]);
print(numbers);
numbers.insert(2, 55);
print(numbers);
numbers.remove(8);
print(numbers);
numbers.removeAt(3) ;
print(numbers);
numbers.removeLast();
print(numbers);
numbers.removeRange(2,5);
print(numbers);
}
Output
[4, 8, 5]
[4, 8, 5, 12]
[4, 8, 5, 12, 15, 16, 18]
[4, 8, 55, 5, 12, 15, 16, 18]
[4, 55, 5, 12, 15, 16, 18]
[4, 55, 5, 15, 16, 18]
[4, 55, 5, 15, 16]
[4, 55]
In the next chapter, we will learn about dart maps.