Education
Software Development Executive - I
Last updated on Sep 6, 2024
Last updated on Oct 23, 2023
In the world of programming, data types are fundamental building blocks that define the nature and behavior of our variables. They allow us to express our intentions clearly and enforce rules that maintain the integrity of our code. Dart, a powerful programming language developed by Google, offers developers a variety of mechanisms to work with different data types. One such mechanism is type casting.
Type casting in Dart is a powerful feature that allows you to convert an object of one data type into another. Dart's type system includes static type checking, which helps to catch and fix errors during compile-time, reducing the chances of runtime errors.
Explicit cast in Dart is a way to convert an object of one type to another. Dart provides the 'as' keyword for this purpose. However, using 'as' for incompatible types can lead to a type error. Let's consider an example:
1void main() { 2 var object = '123'; 3 var string = object as String; 4 print(string); 5}
In this code, we have an object of type 'dynamic' that contains a string '123'. We then cast this object to a string using the 'as' keyword. The print statement will output '123'.
Dart also supports implicit casting, where Dart's type system automatically converts one type to another if it's safe. For instance, an integer can be implicitly cast to a double. Here's an example:
1void main() { 2 int integer = 10; 3 double doubleValue = integer.toDouble(); 4 print(doubleValue); 5}
In this code, we have an integer that we implicitly cast to a double using the toDouble() method. The print statement will output 10.0.
Dart provides several methods to cast types. Let's delve into some examples.
In Dart, you can cast a string to an integer using the int.parse() method. Here's an example:
1void main() { 2 String string = '123'; 3 int integer = int.parse(string); 4 print(integer); 5}
In this code, we have a string '123' that we cast to an integer using the int.parse() method. The print statement will output 123.
Conversely, you can cast an integer to a string using the toString() method. Here's how:
1void main() { 2 int integer = 123; 3 String string = integer.toString(); 4 print(string); 5}
In this code, we have an integer 123 that we cast to a string using the toString() method. The print statement will output '123'.
You can also cast a string to a double (a floating point number) using the double.parse() method. Consider this example:
1void main() { 2 String string = '123.45'; 3 double doubleValue = double.parse(string); 4 print(doubleValue); 5}
In this code, we have a string '123.45' that we cast to a double using the double.parse() method. The print statement will output 123.45.
Lists in Dart are dynamic collections of items. Dart provides the ability to perform type casting with lists, allowing you to create a new list of a specific type from a list with dynamic type elements.
1void main() { 2 List<dynamic> dynamicList = [1, 2, 3]; 3 List<int> intList = dynamicList.cast<int>(); 4 print(intList); 5}
In this code, we have a list of dynamic type elements that we cast to a list of int elements using the cast<int>()
method. The print statement will output [1, 2, 3].
Similarly, you can cast a list of dynamic type elements to a list of string elements. Here's how:
1void main() { 2 List<dynamic> dynamicList = ['a', 'b', 'c']; 3 List<String> stringList = dynamicList.cast<String>(); 4 print(stringList); 5}
In this code, we have a list of dynamic type elements that we cast to a list of string elements using the cast<String>()
method. The print statement will output ['a', 'b', 'c'].
You can also cast a list of dynamic type elements to a list of double elements. Here's an example:
1void main() { 2 List<dynamic> dynamicList = [1.1, 2.2, 3.3]; 3 List<double> doubleList = dynamicList.cast<double>(); 4 print(doubleList); 5}
In this code, we have a list of dynamic type elements that we cast to a list of double elements using the cast<double>()
method. The print statement will output [1.1, 2.2, 3.3].
Generics are a powerful Dart feature that allows you to create a single class or method that works with multiple kinds. This is achieved through the use of type arguments.
Type arguments are placeholders for any type. They are commonly represented by the letter 'T'. You can use type arguments to create classes or methods that can operate on different types. For example, consider the following code:
1class Box<T> { 2 T value; 3 4 Box(this.value); 5 6 T getValue() { 7 return value; 8 } 9}
In the above code, Box is a generic class that can hold a value of any type. The getValue method returns the value of the type argument T.
You can also use type arguments in methods. Consider the following example:
1void main() { 2 printElement<int>(1); 3 printElement<String>('Hello'); 4 printElement<double>(3.14); 5} 6 7void printElement<T>(T element) { 8 print(element); 9}
In this code, printElement is a generic method that takes a parameter of any type and prints it. The void main method calls printElement with different types of arguments.
Generics can also be used with type casting. For example, you can cast a List<dynamic>
to a List<int>
using generics. Here's how:
1void main() { 2 List<dynamic> dynamicList = [1, 2, 3]; 3 List<int> intList = dynamicList.cast<int>(); 4 print(intList); 5}
In this code, dynamicList is a list of dynamic type elements. We cast dynamicList to a list of int elements using the cast<int>()
method. The print statement will output [1, 2, 3].
In conclusion, understanding type casting in Dart is crucial for writing robust and efficient code. Whether you're working with basic types like strings, integers, and doubles, or dealing with collections like lists, Dart's type system offers the flexibility and safety you need.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.