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

# Map Operations

> Practical examples of working with maps in Dart, including dynamic types and iteration

## Working with Maps

This guide explores practical map operations through real-world examples including phone directories and user profiles.

## Basic Map Operations: Phone Directory

Let's create a phone directory using a map:

```dart theme={null}
Map<String, String> telefonos = {
  'Juan': '8764521111',
  'María': '8764521222'
};
```

### Searching for Contacts

Check if a contact exists before accessing it:

```dart theme={null}
if (telefonos.containsKey('Juan')) {
  print('Teléfono de Juan: ${telefonos['Juan']}');
} else {
  print('Contacto No Existe!!!!');
}
```

<Tip>
  Always use `containsKey()` to verify a key exists before accessing it to avoid null values.
</Tip>

### Adding Contacts Dynamically

You can add new entries to a map after creation:

```dart theme={null}
import 'dart:io';

String nombre = '';
for (var i = 0; i < 3; i++) {
  print('Nombre del Contacto:');
  nombre = stdin.readLineSync()!;
  print('Número de Telefono de $nombre:');
  telefonos[nombre] = stdin.readLineSync()!;
}
```

### Iterating Through Maps

Use the `forEach()` method to iterate through all key-value pairs:

```dart theme={null}
print('\nRecorrido de un Diccionario:');
telefonos.forEach((clave, valor){
  print('$clave: $valor');
});
```

**Output:**

```
Recorrido de un Diccionario:
Juan: 8764521111
María: 8764521222
Pedro: 8764523333
```

<Info>
  The `forEach()` method takes a function with two parameters: the key and the value.
</Info>

## Maps with Dynamic Values

Maps can store different types of values using the `dynamic` type:

```dart theme={null}
Map<String, dynamic> usuario = {
  'nombre': 'Roberto',
  'edad': 20,
  'activo': true,
  'calificacion': 8.5
};
```

### Accessing Different Types

```dart theme={null}
print(usuario['nombre']); // Roberto (String)
print(usuario['edad']);  // 20 (int)
print(usuario['activo']); // true (bool)
print(usuario['calificacion']); // 8.5 (double)
```

<Warning>
  When using `dynamic`, Dart can't check types at compile time. Be careful when accessing values to avoid runtime errors.
</Warning>

### Iterating Dynamic Maps

Use `forEach()` with dynamic maps just like regular maps:

```dart theme={null}
print('\nImprimir con ForEach');
usuario.forEach((llave, valor){
  print('$llave: $valor');
});
```

**Output:**

```
Imprimir con ForEach
nombre: Roberto
edad: 20
activo: true
calificacion: 8.5
```

## Complete Examples

### Example 1: Phone Directory

```dart theme={null}
import 'dart:io';

void main() {
  Map<String, String> telefonos = {
    'Juan': '8764521111',
    'María': '8764521222'
  };

  // Buscar
  if (telefonos.containsKey('Juan')) {
    print('Teléfono de Juan: ${telefonos['Juan']}');
  } else 
    print('Contacto No Existe!!!!');

  //Agregar Datos al Diccionario
  String nombre = '';
  for (var i = 0; i < 3; i++) {
    print('Nombre del Contacto:');
    nombre = stdin.readLineSync()!;
    print('Número de Telefono de $nombre:');
    telefonos[nombre] = stdin.readLineSync()!;
  }

  // Iterar
  print('\nRecorrido de un Diccionario:');
  telefonos.forEach((clave, valor){
    print('$clave: $valor');
  });
  print('');
  
}
```

### Example 2: User Profile

```dart theme={null}
void main() {
  Map<String, dynamic> usuario = {
    'nombre': 'Roberto',
    'edad': 20,
    'activo': true,
    'calificacion': 8.5
  };

  print(usuario['nombre']); // Roberto
  print(usuario['edad']);  // 20
  print(usuario['activo']); // true
  print(usuario['calificacion']); // 8.5

  print('\nImprimir con ForEach');
  usuario.forEach((llave, valor){
    print('$llave: $valor');
  });
}
```

## Common Map Operations

| Operation  | Method             | Description           | Example                         |
| ---------- | ------------------ | --------------------- | ------------------------------- |
| Add/Update | `map[key] = value` | Adds or updates entry | `telefonos['Ana'] = '123'`      |
| Search     | `containsKey(key)` | Check if key exists   | `telefonos.containsKey('Juan')` |
| Access     | `map[key]`         | Get value by key      | `telefonos['Juan']`             |
| Iterate    | `forEach()`        | Loop through entries  | `map.forEach((k, v) => ...)`    |
| Remove     | `remove(key)`      | Delete entry          | `telefonos.remove('Juan')`      |

## Iteration Methods Comparison

### Method 1: forEach()

Best for simple iteration:

```dart theme={null}
map.forEach((key, value) {
  print('$key: $value');
});
```

### Method 2: for-in with keys

When you need more control:

```dart theme={null}
for (var key in map.keys) {
  print('$key: ${map[key]}');
}
```

### Method 3: entries

When you need both key and value as objects:

```dart theme={null}
for (var entry in map.entries) {
  print('${entry.key}: ${entry.value}');
}
```

## Use Cases for Different Map Types

<CardGroup cols={2}>
  <Card title="String to String" icon="font">
    Phone directories, translations, configuration settings
  </Card>

  <Card title="String to int" icon="hashtag">
    Ages, scores, quantities, IDs
  </Card>

  <Card title="String to dynamic" icon="shapes">
    User profiles, JSON-like data, flexible configurations
  </Card>

  <Card title="int to String" icon="list-ol">
    Error codes to messages, ID to name mappings
  </Card>
</CardGroup>

## Type Safety Considerations

### Strongly Typed Maps (Recommended)

```dart theme={null}
Map<String, int> ages = {'Alice': 25, 'Bob': 30};
int aliceAge = ages['Alice']!; // Type safe
```

<Tip>
  Use strongly typed maps when all values are the same type for better type safety and IDE support.
</Tip>

### Dynamic Maps (Use with Caution)

```dart theme={null}
Map<String, dynamic> user = {'name': 'Alice', 'age': 25};
String name = user['name'] as String; // Need type casting
int age = user['age'] as int;
```

<Warning>
  With dynamic maps, you lose compile-time type checking and need to cast values. Use only when necessary (e.g., JSON parsing).
</Warning>

## Practical Patterns

### Pattern 1: Safe Access with Default Value

```dart theme={null}
String phone = telefonos['Unknown'] ?? 'No phone available';
```

### Pattern 2: Conditional Update

```dart theme={null}
if (!telefonos.containsKey('Pedro')) {
  telefonos['Pedro'] = '8764525555';
}
```

### Pattern 3: Bulk Operations

```dart theme={null}
Map<String, String> newContacts = {
  'Ana': '111111',
  'Luis': '222222'
};
telefonos.addAll(newContacts);
```

### Pattern 4: Filter and Transform

```dart theme={null}
// Get only active users
Map<String, dynamic> activeUsers = Map.fromEntries(
  usuarios.entries.where((entry) => entry.value['activo'] == true)
);
```

## Best Practices

1. **Use appropriate types**: Choose `Map<String, int>` over `Map<String, dynamic>` when possible
2. **Check before access**: Always use `containsKey()` or null-aware operators
3. **Use forEach() for iteration**: It's more concise and readable than manual loops
4. **Meaningful keys**: Use descriptive key names that represent the data
5. **Handle null values**: Maps return null for non-existent keys, so handle this appropriately

<Note>
  Remember: Map keys must be unique, but values can repeat. If you add an existing key, it will update the value, not create a duplicate.
</Note>
