Skip to content

Commit 0626b1e

Browse files
committed
Add Dart Map .cast() term entry
1 parent 23d0583 commit 0626b1e

File tree

1 file changed

+47
-0
lines changed
  • content/dart/concepts/map/terms/cast

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
Title: '.cast()'
3+
Description: 'Returns a new Map with the same entries but with different key and value types.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Dart'
9+
- 'Map'
10+
- 'Methods'
11+
CatalogContent:
12+
- 'learn-dart'
13+
- 'paths/computer-science'
14+
---
15+
16+
In Dart, the **`.cast()`** method returns a new `Map` containing the same key–value pairs as the original map, but with the specified key and value types. The returned map performs type checks when accessing or modifying its entries.
17+
18+
This method is commonly used when working with dynamically typed maps that need to be treated as a map with specific types.
19+
20+
## Syntax
21+
22+
```pseudo
23+
mapVariable.cast<NewKeyType, NewValueType>()
24+
```
25+
26+
`mapVariable` is the `Map` object whose entries are cast to new types.
27+
28+
## Example
29+
30+
```dart
31+
void main() {
32+
Map<dynamic, dynamic> rawData = {
33+
'Alice': 90,
34+
'Bob': 85,
35+
};
36+
37+
Map<String, int> scores = rawData.cast<String, int>();
38+
39+
print(scores);
40+
}
41+
```
42+
43+
**Output:**
44+
45+
```shell
46+
{Alice: 90, Bob: 85}
47+
```

0 commit comments

Comments
 (0)