.cast()

NBarnhouse's avatar
Published Jul 14, 2024
Contribute to Docs

The .cast() method in Dart is used to change a list type and is used when all elements in a list need to be of a specific data type. It ensures type safety in collections, but all list elements must conform to the specific type that the list is changing to or a TypeError will be thrown at runtime.

Syntax

List<R> newList = originalList.cast<R>();
  • R: Refers to the list type. The most common list types for a typed list are int, double, string, and bool. Besides that, it can also be dynamic.
  • newList: The new list of elements after conforming to a specific type.
  • originalList: The original list of elements that need to be changed.

Examples

In the following example, the .cast() method is used to cast a list containing elements of the same type (int) to a num list:

void main() {
List<int> intList = [14, 25, 36, 47, 58];
print(intList);
List<num> numList = intList.cast<num>();
print(numList);
}

The output of the above code is the same for both intList and numList because num is a type that all numbers conform to, whether they are int or double:

[14, 25, 36, 47, 58]
[14, 25, 36, 47, 58]

Here is another example that uses the .cast() method to cast a dynamic list containing elements of different types to an int list:

void main() {
List<dynamic> dynamicList = [1, 'Two', 'Three', 4, 5];
print(dynamicList);
List<int> intList = dynamicList.cast<int>();
print(intList);
}

The code above will print dynamicList in the output. However, dynamicList will not be successfully cast to intList because a string cannot be changed to an int. Hence, the method returns an error:

[1, Two, Three, 4, 5]
DartPad caught unhandled _TypeError:
TypeError: "Two": type 'String' is not a subtype of type 'int'

All contributors

Contribute to Docs

Learn Dart on Codecademy