Lists are one of the most fundamental data structures in programming. They play a crucial role in Dart applications, allowing developers to store and manipulate collections of data efficiently. In this article, we will explore the basics of lists in Dart, understand the different types of lists, and dive into advanced list operations.

By the end, you will have a comprehensive understanding of lists in Dart and the best practices for using them effectively.

List in Dart

A list in Dart is an ordered collection of objects. It can contain elements of the same type or different types.

Index in Lists

Every value in a list is called element of the list. And every element has is in certain position which is called index. This index starts from 0, suppose there are 2 elements ( also known as values ) in a list. The index of first element will be 0 and the index of second element will be 1 and it goes on like this.

To create a list in Dart, you can use list literals. Here are some examples with the syntax:

Syntax

  
List<data_type> list_name = [value1, value2, value3, ...];
  

Example

  
List<int> numbers = [];
List<String> fruits = ['apple', 'banana', 'orange'];
List mixed = [1, 'two', 3.0]; 


void main() {
  List<String> names = ["John", "Doe", "Smith"];
  print(names);
}
  

Output

  
[John, Doe, Smith]
  

code Try Yourself

Dynamic and Fixed-length Lists:

n Dart there are two types of lists: dynamic (growable) and fixed-length. Dynamic lists automatically grow or shrink as elements are added or removed, while fixed-length lists have a predetermined size.

Dynamic lists offer flexibility as they adapt to the number of elements you add or remove. However, they consume more memory compared to fixed-length lists. On the other hand, fixed-length lists have a fixed size determined during creation. You can create fixed-length lists using the List constructor with a specified length:

Syntax:

  List<data_type> fixedLengthList = List<data_type>(<list_size>); 
  

Example

  List<int> fixedLengthList = List<int>(5); 
  

It’s important to note that once a fixed-length list is created, you cannot change its size. This restriction can be useful in scenarios where you need to ensure a specific number of elements.

List Properties

Properties are the values associated with a Dart class. A list has the following properties:

PropertyDescription
lengthReturns the length of the list
reversedReturns an iterable object containing the list values in the reverse order
isEmptyReturns true if the list is empty
isNotEmptyReturns 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
  

code Try Yourself

List Methods

Methods are the functions associated with a Dart class. A list has the following methods:

MethodDescription
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 condition
retainWhere()Removes all the elements of the list that fail to satisfy the given condition
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 condition
any()Returns true if any of the elements in the list satisfy the given condition
every()Returns true if every element in the list satisfies the given condition
firstWhere()Returns the first element in the list that satisfies the given condition
lastWhere()Returns the last element in the list that satisfies the given condition
singleWhere()Returns the single element in the list that satisfies the given condition
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]
  

code Try Yourself

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]
  

code Try Yourself

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]
  

code Try Yourself

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]
  

code Try Yourself

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]
  

code Try Yourself

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]
  

code Try Yourself

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]
  

code Try Yourself

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); // this will remove element at index 1 and 2
  print(names);
}
  

Output

  
[John, William, Henry]
  

code Try Yourself

removeWhere() in list

The removeWhere() method is used to remove all the elements of the list that satisfy the given condition.

Syntax

  
list_name.removeWhere((element) => condition);
  

Example

  
void main() {
  List<String> names = ["John", "Doe", "Smith", "William", "Henry"];
  
/// We are removing elements that have less than 4 characters
  names.removeWhere((element) => element.length < 4);
  print(names);
}
  

Output

  
[John, Smith, William, Henry]
  

code Try Yourself

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

  
[]
  

code Try Yourself

sort() in list

Sorting in list is fairly easy. 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]
  

code Try Yourself

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]
  

code Try Yourself

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]
  

code Try Yourself

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}
  

code Try Yourself

forEach() in list

The forEach() method is used to apply the given function on each element of the list.

When you want to have access to all the elements in the list you can use for each loop for that reason.

Syntax

  
list_name.forEach((element) => function);
  

Example

  
void main() {
  List<String> names = ["John", "Doe", "Smith"];
  names.forEach((element) => print(element));
}
  

Output

  
John
Doe
Smith
  

code Try Yourself

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 newNames = names.map((element) => element.toUpperCase());
  print(newNames);
}
  

Output

  
(JOHN, DOE, SMITH)
  

code Try Yourself

where() in list

The where() method is used to return a new list containing the elements of the list that satisfy the given condition.

Syntax

  
list_name.where((element) => condition);
  

Example

  
void main() {
  List<String> names = ["John", "Doe", "Smith"];
  var newNames = names.where((element) => element.contains("Sm"));
  print(newNames);
}
  

Output

  
(Smith)
  

code Try Yourself

any()

The any() method is used to return true if any of the elements in the list satisfy the given condition.

This is suitable to check if there are duplicate items.

Syntax

  
list_name.any((element) => condition);
  

Example

  
void main() {
  List<String> names = ["John", "Doe", "Smith"];
  var newNames = names.any((element) => element.contains("Doe"));
  print(newNames);
}
  

Output

  true
  

code Try Yourself

every() in list

The every() method is used to return true if every element in the list satisfies the given condition.

Syntax

  
list_name.every((element) => condition);
  

Example

  
void main() {
  List<String> names = ["John", "Doe", "Smith"];
  var newNames = names.every((element) => element.length < 4);
  print(newNames);
}
  

Output

  
false
  

code Try Yourself

firstWhere() in list

The firstWhere() method is used to return the first element in the list that satisfies the given condition.

Syntax

  
list_name.firstWhere((element) => condition);
  

Example

  
void main() {
  List<String> names = ["John", "Doe", "Smith"];
  var newNames = names.firstWhere((element) => element.length < 4);
  print(newNames);
}
  

Output

  
Doe
  

code Try Yourself

lastWhere() in list

The lastWhere() method is used to return the last element in the list that satisfies the given condition.

Syntax

  
list_name.lastWhere((element) => condition);
  

Example

  void main() {
  List<String> names = ["John", "Doe", "Smith"];
  
  /// this will return the last element that has characters less then 6
  var newNames = names.lastWhere((element) => element.length < 6); 
  
  print(newNames);
}
  

Output

  
Smith
  

code Try Yourself

singleWhere() in list

The singleWhere() method is used to return the single element in the list that satisfies the given condition.

Syntax

  
list_name.singleWhere((element) => condition);
  

Example

  
void main() {
  List<String> names = ["John", "Doe", "Smith", "William", "Henry"];
  var newNames = names.singleWhere((element) => element.length < 4);
  print(newNames);
}
  

Output

  
Doe
  

code Try Yourself

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 newNames = names.join("-");
  print(newNames);
}
  

Output

  
John-Doe-Smith
  

code Try Yourself

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.

We are applying the (value+ element) function. It means if there is a list [1 , 2, 3, 4, 5], it will return the sum of all the elements in a list i.e [1 + 2 + 3 + 4 + 5].

Syntax

  
list_name.reduce((value, element) => function);
  

Example

  
void main() {
  List<int> numbers = [5, 2, 4, 3, 1];
  var totalValue = numbers.reduce((value, element) => value + element);
  print(totalValue);
}
  

Output

  
15
 
  

code Try Yourself

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 index = names.indexOf("Smith");
  print(index);
}
  

Output

  
2
  

code Try Yourself

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", "Smith", "Henry"];
  var lastIndex = names.lastIndexOf("Smith");
  print(lastIndex);
}
  

Output

  
4
  

code Try Yourself

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 hasSmith = names.contains("Smith");
  print(hasSmith);
}
  

Output

  
true
  

code Try Yourself

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 listToSet = names.toSet();
  print(listToSet);
} 
  

Output

  
{John, Doe, Smith, William, Henry}
  

code Try Yourself

To explore more visit List in Dart