Skip to content

Commit 826d306

Browse files
committed
Add README.md file for update-all
1 parent 93f7d7f commit 826d306

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

content/dart/concepts/map/terms/update-all/README.md

Whitespace-only changes.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
Title: '.updateAll()'
3+
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}
54+
Updated scores: {Alice: 90, Bob: 100, Charlie: 95}
55+
```

0 commit comments

Comments
 (0)