Skip to main content

What are Lists?

Lists in Dart are ordered collections of items. They are similar to arrays in other programming languages and can dynamically grow or shrink in size.

Creating Lists

You can create a list with a specific type using type annotations:
List<int> numeros = [10, 20, 20, 30];
print(numeros); // [10, 20, 20, 30]
Lists in Dart can contain duplicate values, as shown in the example above where 20 appears twice.

Adding Elements

There are several methods to add elements to a list:

Adding a Single Element

Use the add() method to append an element to the end of the list:
numeros.add(40);
print(numeros); // [10, 20, 20, 30, 40]

Adding Multiple Elements

Use the addAll() method to append multiple elements at once:
numeros.addAll([50, 60]);
print(numeros); // [10, 20, 20, 30, 40, 50, 60]

Removing Elements

Dart provides multiple ways to remove elements from a list:

Remove by Value

The remove() method removes the first occurrence of a specific value:
numeros.remove(20); // Removes the first 20
print(numeros); // [10, 20, 30, 40, 50, 60]

numeros.remove(20); // Removes the second 20
print(numeros); // [10, 30, 40, 50, 60]

numeros.remove(267540); // No error if value doesn't exist
print(numeros); // [10, 30, 40, 50, 60]
The remove() method only removes the first occurrence of a value. If you need to remove all occurrences, you’ll need to call it multiple times or use a different approach.

Remove by Index

The removeAt() method removes an element at a specific position:
numeros.removeAt(0); // Removes element at index 0
print(numeros); // [30, 40, 50, 60]

Accessing Elements

Access individual elements using bracket notation with the index (starting from 0):
print(numeros[0]); // First element
print(numeros[2]); // Third element

List Properties

Dart lists provide several useful properties for querying information:
PropertyDescriptionExample
lengthReturns the number of elementsnumeros.length
isEmptyReturns true if the list is emptynumeros.isEmpty
isNotEmptyReturns true if the list has elementsnumeros.isNotEmpty
firstReturns the first elementnumeros.first
lastReturns the last elementnumeros.last
print('Tamaño de la Lista: ${numeros.length}');
print('Esta Vacia?: ${numeros.isEmpty}');
print('No esta vacia?: ${numeros.isNotEmpty}');
print('Primer valor: ${numeros.first}');
print('Último valor: ${numeros.last}');

Searching in Lists

Check if Element Exists

Use contains() to check if a value exists:
print('Existe?: ${numeros.contains(40)}'); // true or false

Find Element Position

Use indexOf() to find the position of an element:
print('Posición: ${numeros.indexOf(40)}'); // Returns index or -1 if not found
The indexOf() method returns -1 if the element is not found, making it useful for conditional checks.

Safe Element Removal

Combine indexOf() with conditional logic for safe removal:
if (numeros.indexOf(40) >= 0) {
  numeros.removeAt(numeros.indexOf(40));
  print('Elemento borrado');
  print(numeros);
} else {
  print('Número no Existe');
}

Complete Example

void main() {
  List<int> numeros = [10, 20, 20, 30];
  print(numeros);

  // AGREGAR elementos
  numeros.add(40);
  print(numeros);
  numeros.addAll([50, 60]);
  print(numeros);

  // ELIMINAR elementos
  numeros.remove(20);
  print(numeros);
  numeros.removeAt(0);
  print(numeros);

  // CONSULTAR
  print('Tamaño de la Lista: ${numeros.length}');
  print('Primer valor: ${numeros.first}');
  print('Último valor: ${numeros.last}');

  // BÚSQUEDA
  print('Existe?: ${numeros.contains(40)}');
  print('Posición: ${numeros.indexOf(40)}');
}

Key Takeaways

Dynamic Size

Lists can grow and shrink dynamically using add(), addAll(), remove(), and removeAt()

Type Safe

Use type annotations like List<int> to ensure all elements are of the same type

Zero-Indexed

List indices start at 0, so the first element is at position 0

Rich API

Dart provides many built-in methods and properties for working with lists

Build docs developers (and LLMs) love