|
| 1 | +--- |
| 2 | +Title: '.update()' |
| 3 | +Description: 'Updates the value for a specific key in a Map using a provided function.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Code Foundations' |
| 7 | +Tags: |
| 8 | + - 'Dart' |
| 9 | + - 'Methods' |
| 10 | + - 'Map' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-dart' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +In Dart, the **`.update()`** method is used to update the value for a specific key in a `Map`. It takes a key and a function that computes the new value based on the existing value. If the key does not exist, an optional `ifAbsent` function can be provided to add a new key-value pair. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +mapVariable.update(key, (value) => newValue) |
| 22 | +``` |
| 23 | + |
| 24 | +- `mapVariable`: The `Map` to be updated. |
| 25 | +- `key`: The key whose value needs to be updated. |
| 26 | +- `value`: The current value associated with the key. |
| 27 | +- `newValue`: The new value to replace the existing value. |
| 28 | + |
| 29 | +An optional `ifAbsent` parameter can be used to add a key if it does not exist: |
| 30 | + |
| 31 | +```pseudo |
| 32 | +mapVariable.update(key, (value) => newValue, ifAbsent: () => defaultValue) |
| 33 | +``` |
| 34 | + |
| 35 | +- `ifAbsent`: A function that returns a default value if the key is not found. |
| 36 | + |
| 37 | +## Example 1 |
| 38 | + |
| 39 | +In the following example, the `.update()` method is used to update the value for an existing key in a `Map`: |
| 40 | + |
| 41 | +```dart |
| 42 | +void main() { |
| 43 | + Map<String, int> fruits = {'Apple': 5, 'Banana': 3}; |
| 44 | +
|
| 45 | + // Update the value for 'Apple' |
| 46 | + fruits.update('Apple', (value) => value + 2); |
| 47 | +
|
| 48 | + print(fruits); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +Here is the output for the above example: |
| 53 | + |
| 54 | +```shell |
| 55 | +{Apple: 7, Banana: 3} |
| 56 | +``` |
| 57 | + |
| 58 | +## Example 2 |
| 59 | + |
| 60 | +The `.update()` method can use the `ifAbsent` parameter to add a key if it does not exist: |
| 61 | + |
| 62 | +```dart |
| 63 | +void main() { |
| 64 | + Map<String, int> fruits = {'Apple': 5}; |
| 65 | +
|
| 66 | + // 'Orange' does not exist, so ifAbsent adds it |
| 67 | + fruits.update('Orange', (value) => value, ifAbsent: () => 10); |
| 68 | +
|
| 69 | + print(fruits); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Here is the output for the above example: |
| 74 | + |
| 75 | +```shell |
| 76 | +{Apple: 5, Orange: 10} |
| 77 | +``` |
| 78 | + |
| 79 | +## Example 3 |
| 80 | + |
| 81 | +The `.update()` method can also work with different [data types](https://www.codecademy.com/resources/docs/dart/data-types) for keys and values: |
| 82 | + |
| 83 | +```dart |
| 84 | +void main() { |
| 85 | + Map<int, String> students = {1: 'Alice', 2: 'Bob'}; |
| 86 | +
|
| 87 | + // Update the value for key 1 |
| 88 | + students.update(1, (value) => '$value Smith'); |
| 89 | +
|
| 90 | + print(students); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +Here is the output for the above example: |
| 95 | + |
| 96 | +```shell |
| 97 | +{1: Alice Smith, 2: Bob} |
| 98 | +``` |
0 commit comments