> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/NormandoRamirezDelgado/6C-Febrero-2026/llms.txt
> Use this file to discover all available pages before exploring further.

# Lists

> Learn about lists in Dart - dynamic arrays that can store multiple values

## 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:

```dart theme={null}
List<int> numeros = [10, 20, 20, 30];
print(numeros); // [10, 20, 20, 30]
```

<Note>
  Lists in Dart can contain duplicate values, as shown in the example above where `20` appears twice.
</Note>

## 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:

```dart theme={null}
numeros.add(40);
print(numeros); // [10, 20, 20, 30, 40]
```

### Adding Multiple Elements

Use the `addAll()` method to append multiple elements at once:

```dart theme={null}
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:

```dart theme={null}
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]
```

<Warning>
  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.
</Warning>

### Remove by Index

The `removeAt()` method removes an element at a specific position:

```dart theme={null}
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):

```dart theme={null}
print(numeros[0]); // First element
print(numeros[2]); // Third element
```

## List Properties

Dart lists provide several useful properties for querying information:

| Property     | Description                             | Example              |
| ------------ | --------------------------------------- | -------------------- |
| `length`     | Returns the number of elements          | `numeros.length`     |
| `isEmpty`    | Returns `true` if the list is empty     | `numeros.isEmpty`    |
| `isNotEmpty` | Returns `true` if the list has elements | `numeros.isNotEmpty` |
| `first`      | Returns the first element               | `numeros.first`      |
| `last`       | Returns the last element                | `numeros.last`       |

```dart theme={null}
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:

```dart theme={null}
print('Existe?: ${numeros.contains(40)}'); // true or false
```

### Find Element Position

Use `indexOf()` to find the position of an element:

```dart theme={null}
print('Posición: ${numeros.indexOf(40)}'); // Returns index or -1 if not found
```

<Tip>
  The `indexOf()` method returns `-1` if the element is not found, making it useful for conditional checks.
</Tip>

### Safe Element Removal

Combine `indexOf()` with conditional logic for safe removal:

```dart theme={null}
if (numeros.indexOf(40) >= 0) {
  numeros.removeAt(numeros.indexOf(40));
  print('Elemento borrado');
  print(numeros);
} else {
  print('Número no Existe');
}
```

## Complete Example

```dart theme={null}
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

<CardGroup cols={2}>
  <Card title="Dynamic Size" icon="arrows-up-down">
    Lists can grow and shrink dynamically using `add()`, `addAll()`, `remove()`, and `removeAt()`
  </Card>

  <Card title="Type Safe" icon="shield-check">
    Use type annotations like `List<int>` to ensure all elements are of the same type
  </Card>

  <Card title="Zero-Indexed" icon="list-ol">
    List indices start at 0, so the first element is at position 0
  </Card>

  <Card title="Rich API" icon="code">
    Dart provides many built-in methods and properties for working with lists
  </Card>
</CardGroup>
