From 0626b1e346778c2d0d86c4d40d82000223682e5b Mon Sep 17 00:00:00 2001 From: Muntazir-sd Date: Fri, 19 Dec 2025 20:50:27 +0530 Subject: [PATCH 1/2] Add Dart Map .cast() term entry --- content/dart/concepts/map/terms/cast/cast.md | 47 ++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 content/dart/concepts/map/terms/cast/cast.md diff --git a/content/dart/concepts/map/terms/cast/cast.md b/content/dart/concepts/map/terms/cast/cast.md new file mode 100644 index 00000000000..d685753a4fb --- /dev/null +++ b/content/dart/concepts/map/terms/cast/cast.md @@ -0,0 +1,47 @@ +--- +Title: '.cast()' +Description: 'Returns a new Map with the same entries but with different key and value 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() +``` + +`mapVariable` is the `Map` object whose entries are cast to new types. + +## Example + +```dart +void main() { + Map rawData = { + 'Alice': 90, + 'Bob': 85, + }; + + Map scores = rawData.cast(); + + print(scores); +} +``` + +**Output:** + +```shell +{Alice: 90, Bob: 85} +``` From dfd81bd760f82021aafd7f59d25a868b7afddce5 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Mon, 22 Dec 2025 15:57:52 +0530 Subject: [PATCH 2/2] Clarify description of .cast() method Updated the description to clarify that keys and values are cast to specified types. --- content/dart/concepts/map/terms/cast/cast.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/content/dart/concepts/map/terms/cast/cast.md b/content/dart/concepts/map/terms/cast/cast.md index d685753a4fb..6ce4fae6721 100644 --- a/content/dart/concepts/map/terms/cast/cast.md +++ b/content/dart/concepts/map/terms/cast/cast.md @@ -1,6 +1,6 @@ --- Title: '.cast()' -Description: 'Returns a new Map with the same entries but with different key and value types.' +Description: 'Returns a new map with the same entries but with keys and values cast to the specified types.' Subjects: - 'Computer Science' - 'Data Science' @@ -25,8 +25,19 @@ mapVariable.cast() `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` 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` so its entries can be accessed with strong typing: + ```dart void main() { Map rawData = { @@ -40,7 +51,7 @@ void main() { } ``` -**Output:** +The output of this code is: ```shell {Alice: 90, Bob: 85}