Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions content/dart/concepts/map/terms/cast/cast.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
Title: '.cast()'
Description: 'Returns a new map with the same entries but with keys and values cast to the specified types.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Dart'
- 'Map'
- 'Methods'
CatalogContent:
- 'learn-dart'
- 'paths/computer-science'
---

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.

This method is commonly used when working with dynamically typed maps that need to be treated as a map with specific types.

## Syntax

```pseudo
mapVariable.cast<NewKeyType, NewValueType>()
```

`mapVariable` is the `Map` object whose entries are cast to new types.

**Parameters:**

- `NewKeyType`: The target key type to cast the map’s keys to.
- `NewValueType`: The target value type to cast the map’s values to.

**Return value:**

Returns a new `Map<K, V>` view of the original map, where keys and values are cast to the specified types.

## Example

In this example, a dynamically typed map is cast to a `Map<String, int>` so its entries can be accessed with strong typing:

```dart
void main() {
Map<dynamic, dynamic> rawData = {
'Alice': 90,
'Bob': 85,
};

Map<String, int> scores = rawData.cast<String, int>();

print(scores);
}
```

The output of this code is:

```shell
{Alice: 90, Bob: 85}
```