You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: 'Updates all values in a map by applying a function to each key-value pair.'
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
+
The **`.updateAll()`** method in Dart updates all values in a map by applying a specified function to each key-value pair. The function receives both the key and its current value, and returns the new value that will replace the existing one.
17
+
18
+
## Syntax
19
+
20
+
```dart
21
+
map.updateAll((key, value) => newValue)
22
+
```
23
+
24
+
-`map`: The map to be updated.
25
+
-`key`: The key in each key-value pair.
26
+
-`value`: The current value associated with the key.
27
+
-`newValue`: The new value to replace the current value.
28
+
29
+
## Example
30
+
31
+
In the following example, the `.updateAll()` method is used to increase all scores by 10:
32
+
33
+
```dart
34
+
void main() {
35
+
Map<String, int> scores = {
36
+
'Alice': 80,
37
+
'Bob': 90,
38
+
'Charlie': 85
39
+
};
40
+
41
+
print('Original scores: $scores');
42
+
43
+
// Increase all scores by 10
44
+
scores.updateAll((name, score) => score + 10);
45
+
46
+
print('Updated scores: $scores');
47
+
}
48
+
```
49
+
50
+
The above code produces the following output:
51
+
52
+
```shell
53
+
Original scores: {Alice: 80, Bob: 90, Charlie: 85}
0 commit comments