Type Promotion in Dart
Type Promotion In Dart
Type promotion in dart means that dart automatically converts a value of one type to another type. Dart does this when it knows that the value is of a specific type.
How Type Promotion Works In Dart?
Types Promotion in Dart works in the following ways:
- Promoting from general types to specific subtypes.
- Promoting from nullable types to non-nullable types.
Example 1: Promoting From General Types To Specific Subtypes
In this example, the variable name is declared as an Object. The Object class doesn’t have a .length property. Variable name gets promoted from Object to String so that you can access the .length property of the String class.
void main(){
Object name = "Pratik";
// print(name.length) will not work because Dart doesn't know that name is a String
if(name is String) {
// name promoted from Object to String
print("The length of name is ${name.length}");
}
}
Run Online
Example 2: Type Promotion In Dart
In this example, the variable result is declared as a String. In both if and else blocks, the variable result is assigned a value of type String. Therefore, the variable result is automatically promoted to a non-nullable type String.
void main(){
// result is a String
String result;
// result is promoted to a non-nullable type String
if(DateTime.now().hour < 12) {
result = "Good Morning";
} else {
result = "Good Afternoon";
}
// display the result
print("Result is $result");
print("Length of result is ${result.length}");
}
Run Online
Example 3: Type Promotion With Nullable To Non-Nullable Type
In Dart, you can also throw an exception if the variable is null. In this example, method printLength, takes a String type parameter. If the parameter is null, then it will throw an exception.
// method to print the length of the text
void printLength(String? text){
if(text == null) {
throw Exception("The text is null");
}
print("Length of text is ${text.length}");
}
// main method
void main() {
printLength("Hello");
}
Run Online
Example 4: Type Promotion With Nullable Type To Non-Nullable Type
In this example, the variable value contains a value of type String or null. The variable value is promoted to a non-nullable type String in the if block. If the variable value is null, then the else block is executed.
// importing dart:math library
import 'dart:math';
// creating a class DataProvider
class DataProvider{
// creating a method stringorNull
String? get stringorNull => Random().nextBool() ? "Hello" : null;
// creating a method myMethod
void myMethod(){
String? value = stringorNull;
// checking if value String or not
if(value is String){
print("The length of value is ${value.length}");
}else{
print("The value is not string.");
}
}
}
// main method
void main() {
DataProvider().myMethod();
}
Run Online
Note: The output of the above example is random. It can be either The length of value is 5 or The value is not string.
Video
Watch our video on the type promotion in Dart.