Type Conversion

Published Apr 22, 2024
Contribute to Docs

In Dart, type conversion refers to the process of converting a value from one data type to another. Dart supports both implicit and explicit type conversion.

Implicit Type Conversion

Implicit type conversion is done automatically when operations involving different numeric types are performed.

Example

In the following example, the data type of intValue is implicitly converted from an int (10) to a double (10.0) to perform the addition with doubleValue:

void main() {
int intValue = 10;
double doubleValue = .5;
// Demonstrating implicit type conversion during arithmetic operation
double result = intValue + doubleValue;
print(result);
}

This produces the following output:

10.5

Explicit Type Conversion

Explicit type conversion or type casting is done by programmers using predefined functions or methods to convert values from one type to another. The following are the methods for explicit type conversion:

  • toString(): Converts a value to a string.
  • toDouble(): Converts a value to a double.
  • toInt(): Converts a value to an integer.
  • parse(): Converts a string to a numeric type.
  • tryParse(): Similar to parse(), but returns null if the conversion fails.

Example

In this example, the toString() method is explicitly called on intValue to convert the default integer type to a string:

void main() {
var intValue = 42;
var stringValue = intValue.toString(); // Explicit toString() call
print(stringValue);
}

The output is as follows:

42

Type Conversion

.toDouble()
Returns the double representation of the numerical value.
.toInt()
Converts numeric values to integers.
.toString()
Converts the object into a string representation.
.toStringAsFixed()
Converts a number to a string with a fixed number of decimal places.
.tryParse()
Parses a string and converts it into the given data type.

All contributors

Looking to contribute?

Learn Dart on Codecademy