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.


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;

This produces the following output:


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.


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

The output is as follows:


Type Conversion

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

All contributors

Looking to contribute?

Learn Dart on Codecademy