Dart Type Conversion
Dart Type Conversion
Type conversion means changing a value from one type to another type.
For example, you can change the string '25' into the number 25.
Dart does not change types for you.
If you mix a string and a number with +, the program does not run:
Result:
main.dart:2:19: Error: A value of type 'int' can't be assigned to a variable of type 'String'.
print('Age: ' + 25);
^
You must convert the value yourself, with the methods on this page.
String to Number
Use int.parse() to convert a string to an integer.
Use double.parse() to convert a string to a decimal number:
Example
void main() {
int age = int.parse('25');
double price = double.parse('9.99');
print(age + 1);
print(price * 2);
}
Try it Yourself »
Result:
26
19.98
If the string is not a valid number, parse() stops the program with a FormatException error:
Result:
Unhandled exception:
FormatException: Invalid radix-10 number (at character 1)
abc
^
Note: int.parse('3.5') also fails, because 3.5 is not a whole number.
Safe Conversion with tryParse()
int.tryParse() and double.tryParse() work like parse(), but they never stop the program.
If the string is not a valid number, they return null, which means "no value":
Example
void main() {
print(int.tryParse('42'));
print(int.tryParse('abc'));
print(double.tryParse('2.5'));
print(double.tryParse('hello'));
}
Try it Yourself »
Result:
42
null
2.5
null
Use tryParse() when the text comes from a user, because users can type anything.
You will learn how to work with null in the next chapter, Null Safety.
String to Boolean
Use bool.parse() to convert the text 'true' or 'false' to a bool:
Example
void main() {
bool isMember = bool.parse('true');
bool isAdmin = bool.parse('false');
print(isMember);
print(isAdmin);
print(bool.tryParse('yes'));
}
Try it Yourself »
Result:
true
false
null
The text must be exactly 'true' or 'false', in small letters.
Other text, like 'yes' or 'True', makes bool.parse() stop the program, and bool.tryParse() return null.
Number to String
Use the toString() method to convert a number to a string.
After that, you can join it with other strings:
Example
void main() {
int score = 95;
String text = 'Score: ' + score.toString();
print(text);
}
Try it Yourself »
Result:
Score: 95
Use toStringAsFixed() to get a string with a fixed number of decimals:
Example
void main() {
double price = 4.5678;
print(price.toStringAsFixed(2));
print(price.toStringAsFixed(0));
}
Try it Yourself »
Result:
4.57
5
Tip: String interpolation, like 'Score: $score', also converts the number to a string for you.
int and double
Use toDouble() to convert an int to a double.
Use toInt() to convert a double to an int.
toInt() removes the decimals, it does not round:
Example
void main() {
int apples = 5;
double weight = 7.9;
print(apples.toDouble());
print(weight.toInt());
}
Try it Yourself »
Result:
5.0
7
To round to the nearest whole number, use round().
Use ceil() to round up, and floor() to round down:
Example
void main() {
double x = 7.5;
print(x.round());
print(x.ceil());
print(x.floor());
}
Try it Yourself »
Result:
8
8
7
A num can hold an int or a double.
Use toInt() when you need an int from a num:
Example
void main() {
num temperature = 21.8;
int whole = temperature.toInt();
print(whole);
num count = num.parse('12');
print(count.toInt() + 3);
}
Try it Yourself »
Result:
21
15