List in Dart
Lists are used to store multiple items in a single variable. Lists are a type of collection in Dart. Lists can be declared in two ways. List are mutable, which means, they can be changed after they have been created. List are similar to arrays in other programming languages.
Syntax
List<data_type> list_name = [value1, value2, value3, ...];
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
print(names);
}
Output
[John, Doe, Smith]
List Properties
Properties are the values associated with a Dart class. A list has the following properties:
Property | Description |
---|---|
length | Returns the length of the list |
reversed | Returns an iterable object containing the list values in the reverse order |
isEmpty | Returns true if the list is empty |
isNotEmpty | Returns true if the list is not empty |
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
print(names.length);
print(names.reversed);
print(names.isEmpty);
print(names.isNotEmpty);
}
Output
3
(Smith, Doe, John)
false
true
List Methods
Methods are the functions associated with a Dart class. A list has the following methods:
Method | Description |
---|---|
add() | Adds an element at the end of the list |
addAll() | Adds all the elements of a list to the current list |
insert() | Inserts an element at the given index in the list |
insertAll() | Inserts all the elements of a list to the current list starting from the given index |
remove() | Removes the first occurrence of the specified element in the list |
removeAt() | Removes the element at the specified index from the list |
removeLast() | Removes the last element from the list |
removeRange() | Removes all the elements in the given range from the list |
removeWhere() | Removes all the elements of the list that satisfy the given predicate |
retainWhere() | Removes all the elements of the list that fail to satisfy the given predicate |
clear() | Removes all the elements from the list |
sort() | Sorts the elements of the list in ascending order |
sublist() | Returns a new list containing the elements of the list from the given start index to the end index |
shuffle() | Randomly shuffles the elements of the list |
asMap() | Returns a map view of the list where keys are the indices and values are the corresponding elements |
forEach() | Applies the given function on each element of the list |
map() | Returns a new list containing the results of applying the given function on each element of the list |
where() | Returns a new list containing the elements of the list that satisfy the given predicate |
any() | Returns true if any of the elements in the list satisfy the given predicate |
every() | Returns true if every element in the list satisfies the given predicate |
firstWhere() | Returns the first element in the list that satisfies the given predicate |
lastWhere() | Returns the last element in the list that satisfies the given predicate |
singleWhere() | Returns the single element in the list that satisfies the given predicate |
join() | Concatenates all the elements of the list to a string |
reduce() | Applies the given function on each element of the list in left-to-right order and returns a single value |
fold() | Applies the given function on each element of the list in left-to-right order and returns a single value |
indexOf() | Returns the first index of the given element in the list |
lastIndexOf() | Returns the last index of the given element in the list |
contains() | Returns true if the list contains the given element |
toSet() | Returns a set containing the elements of the list |
getRange() | Returns an iterable object containing the elements of the list from the given start index to the end index |
setRange() | Replaces the elements in the given range in the list with the elements of the given iterable object |
replaceRange() | Replaces the elements in the given range in the list with the given value |
fillRange() | Replaces the elements in the given range in the list with the given value |
Now, Lets understand each method with an example.
add() in list
The add() method is used to add an element at the end of the list.
Syntax
list_name.add(element);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
names.add("William");
print(names);
}
Output
[John, Doe, Smith, William]
addAll() in list
The addAll() method is used to add all the elements of a list to the current list.
Syntax
list_name.addAll(list_name);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
List<String> names2 = ["William", "Henry"];
names.addAll(names2);
print(names);
}
Output
[John, Doe, Smith, William, Henry]
insert() in list
The insert() method is used to insert an element at the given index in the list.
Syntax
list_name.insert(index, element);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
names.insert(1, "William");
print(names);
}
Output
[John, William, Doe, Smith]
insertAll() in list
The insertAll() method is used to insert all the elements of a list to the current list starting from the given index.
Syntax
list_name.insertAll(index, list_name);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
List<String> names2 = ["William", "Henry"];
names.insertAll(1, names2);
print(names);
}
Output
[John, William, Henry, Doe, Smith]
remove() in List
The remove() method is used to remove the first occurrence of the specified element in the list.
Syntax
list_name.remove(element);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
names.remove("Doe");
print(names);
}
Output
[John, Smith]
removeAt() in list
The removeAt() method is used to remove the element at the specified index from the list.
Syntax
list_name.removeAt(index);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
names.removeAt(1);
print(names);
}
Output
[John, Smith]
removeLast() in list
The removeLast() method is used to remove the last element from the list.
Syntax
list_name.removeLast();
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
names.removeLast();
print(names);
}
Output
[John, Doe]
removeRange() in list
The removeRange() method is used to remove all the elements in the given range from the list.
Syntax
list_name.removeRange(start_index, end_index);
Example
void main() {
List<String> names = ["John", "Doe", "Smith", "William", "Henry"];
names.removeRange(1, 3);
print(names);
}
Output
[John, William, Henry]
removeWhere() in list
The removeWhere() method is used to remove all the elements of the list that satisfy the given predicate.
Syntax
list_name.removeWhere((element) => condition);
Example
void main() {
List<String> names = ["John", "Doe", "Smith", "William", "Henry"];
names.removeWhere((element) => element.length < 4);
print(names);
}
Output
[John, Smith, William, Henry]
retainWhere() in list
The retainWhere() method is used to remove all the elements of the list that fail to satisfy the given predicate.
Syntax
list_name.retainWhere((element) => condition);
Example
void main() {
List<String> names = ["John", "Doe", "Smith", "William", "Henry"];
names.retainWhere((element) => element.length < 4);
print(names);
}
Output
[Doe]
clear() in list
The clear() method is used to remove all the elements from the list.
Syntax
list_name.clear();
Example
void main() {
List<String> names = ["John", "Doe", "Smith", "William", "Henry"];
names.clear();
print(names);
}
Output
[]
sort() in list
The sort() method is used to sort the elements of the list in ascending order.
Syntax
list_name.sort();
Example
void main() {
List<int> numbers = [5, 2, 4, 3, 1];
numbers.sort();
print(numbers);
}
Output
[1, 2, 3, 4, 5]
sublist() in list
The sublist() method is used to return a new list containing the elements of the list from the given start index to the end index.
Syntax
list_name.sublist(start_index, end_index);
Example
void main() {
List<int> numbers = [5, 2, 4, 3, 1];
print(numbers.sublist(1, 3));
}
Output
[2, 4]
shuffle() in list
The shuffle() method is used to randomly shuffle the elements of the list.
Syntax
list_name.shuffle();
Example
void main() {
List<int> numbers = [5, 2, 4, 3, 1];
numbers.shuffle();
print(numbers);
}
Output
[2, 5, 1, 3, 4]
asMap() in list
The asMap() method is used to return a map view of the list where keys are the indices and values are the corresponding elements.
Syntax
list_name.asMap();
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
print(names.asMap());
}
Output
{0: John, 1: Doe, 2: Smith}
forEach() in list
The forEach() method is used to apply the given function on each element of the list.
Syntax
list_name.forEach((element) => function);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
names.forEach((element) => print(element));
}
Output
John
Doe
Smith
map() in list
The map() method is used to return a new list containing the results of applying the given function on each element of the list.
Syntax
list_name.map((element) => function);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
var new_names = names.map((element) => element.toUpperCase());
print(new_names);
}
Output
(JOHN, DOE, SMITH)
where() in list
The where() method is used to return a new list containing the elements of the list that satisfy the given predicate.
Syntax
list_name.where((element) => condition);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
var new_names = names.where((element) => element.length < 4);
print(new_names);
}
Output
(John, Doe)
any()
The any() method is used to return true if any of the elements in the list satisfy the given predicate.
Syntax
list_name.any((element) => condition);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
var new_names = names.any((element) => element.length < 4);
print(new_names);
}
Output
true
every() in list
The every() method is used to return true if every element in the list satisfies the given predicate.
Syntax
list_name.every((element) => condition);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
var new_names = names.every((element) => element.length < 4);
print(new_names);
}
Output
false
firstWhere() in list
The firstWhere() method is used to return the first element in the list that satisfies the given predicate.
Syntax
list_name.firstWhere((element) => condition);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
var new_names = names.firstWhere((element) => element.length < 4);
print(new_names);
}
Output
John
lastWhere() in list
The lastWhere() method is used to return the last element in the list that satisfies the given predicate.
Syntax
list_name.lastWhere((element) => condition);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
var new_names = names.lastWhere((element) => element.length < 4);
print(new_names);
}
Output
Smith
singleWhere() in list
The singleWhere() method is used to return the single element in the list that satisfies the given predicate.
Syntax
list_name.singleWhere((element) => condition);
Example
void main() {
List<String> names = ["John", "Doe", "Smith", "William", "Henry"];
var new_names = names.singleWhere((element) => element.length < 4);
print(new_names);
}
Output
Doe
join() in list
The join() method is used to concatenate all the elements of the list to a string.
Syntax
list_name.join(separator);
Example
void main() {
List<String> names = ["John", "Doe", "Smith"];
var new_names = names.join(" ");
print(new_names);
}
Output
John Doe Smith
reduce() in list
The reduce() method is used to apply the given function on each element of the list in left-to-right order and returns a single value.
Syntax
list_name.reduce((value, element) => function);
Example
void main() {
List<int> numbers = [5, 2, 4, 3, 1];
var new_numbers = numbers.reduce((value, element) => value + element);
print(new_numbers);
}
Output
15
fold() in list
The fold() method is used to apply the given function on each element of the list in left-to-right order and returns a single value.
Syntax
list_name.fold(initial_value, (value, element) => function);
Example
void main() {
List<int> numbers = [5, 2, 4, 3, 1];
var new_numbers = numbers.fold(0, (value, element) => value + element);
print(new_numbers);
}
Output
15
indexOf() in list
The indexOf() method is used to return the first index of the given element in the list.
Syntax
list_name.indexOf(element);
Example
void main() {
List<String> names = ["John", "Doe", "Smith", "William", "Henry"];
var new_names = names.indexOf("Smith");
print(new_names);
}
Output
2
lastIndexOf() in list
The lastIndexOf() method is used to return the last index of the given element in the list.
Syntax
list_name.lastIndexOf(element);
Example
void main() {
List<String> names = ["John", "Doe", "Smith", "William", "Henry"];
var new_names = names.lastIndexOf("Smith");
print(new_names);
}
Output
2
contains() in list
The contains() method is used to return true if the list contains the given element.
Syntax
list_name.contains(element);
Example
void main() {
List<String> names = ["John", "Doe", "Smith", "William", "Henry"];
var new_names = names.contains("Smith");
print(new_names);
}
Output
true
toSet() in list
The toSet() method is used to return a set containing the elements of the list.
Syntax
list_name.toSet();
Example
void main() {
List<String> names = ["John", "Doe", "Smith", "William", "Henry"];
var new_names = names.toSet();
print(new_names);
}
Output
{John, Doe, Smith, William, Henry}
getRange() in list
The getRange() method is used to return an iterable object containing the elements of the list from the given start index to the end index.
Syntax
list_name.getRange(start_index, end_index);
Example
void main() {
List<int> numbers = [5, 2, 4, 3, 1];
var new_numbers = numbers.getRange(1, 3);
print(new_numbers);
}
Output
(2, 4)
setRange() in list
The setRange() method is used to replace the elements in the given range in the list with the elements of the given iterable object.
Syntax
list_name.setRange(start_index, end_index, iterable_object);
Example
void main() {
List<int> numbers = [5, 2, 4, 3, 1];
numbers.setRange(1, 3, [6, 7]);
print(numbers);
}
Output
[5, 6, 7, 3, 1]
replaceRange() in list
The replaceRange() method is used to replace the elements in the given range in the list with the given value.
Syntax
list_name.replaceRange(start_index, end_index, value);
Conclusion
In this tutorial, we have learnt about Lists in Dart. We have learnt about the properties and methods of Lists in Dart.
Contributing
Contributions are welcome!