Dart .cast()
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 areint,double,string, andbool. Besides that, it can also bedynamic.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'
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Dart on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours