Dart Type Conversion


Type conversion in Dart refers to the process of converting a value from one data type to another. This is often necessary when working with different types of variables and expressions, allowing you to ensure that your code behaves as expected. Dart supports both implicit and explicit type conversion.

1. Implicit Type Conversion

Dart performs implicit type conversion when it is safe and logical to do so. This usually happens with numeric types, where a smaller type can be automatically converted to a larger type without losing information. For example, an int can be implicitly converted to a double.

Example:

void main() { int intValue = 42; double doubleValue = intValue; // Implicit conversion from int to double print(doubleValue); // Output: 42.0 }

In this example, the int value 42 is implicitly converted to a double without any issues.

2. Explicit Type Conversion

Explicit type conversion occurs when you need to convert a value from one type to another manually. This is done using type casting or factory constructors. Dart provides several ways to perform explicit type conversion, including using as, the toString() method, and factory constructors for specific classes.

2.1 Using as Operator

The as operator is used to cast an object to a specified type. If the object cannot be cast to the desired type, a TypeError will be thrown at runtime.

Example:

void main() { Object obj = 'Hello, Dart!'; // obj is of type Object // Casting Object to String String str = obj as String; print(str); // Output: Hello, Dart! }

2.2 Using toString() Method

You can convert numeric types to strings using the toString() method, which is available on all objects.

Example:

void main() { int intValue = 42; // Convert int to String String strValue = intValue.toString(); print(strValue); // Output: "42" }

2.3 Using double.parse() and int.parse()

You can convert strings to numeric types using the parse method, which is available on the int and double classes.

Example:

void main() { String strInt = '42'; String strDouble = '3.14'; // Convert String to int int intValue = int.parse(strInt); print(intValue); // Output: 42 // Convert String to double double doubleValue = double.parse(strDouble); print(doubleValue); // Output: 3.14 }

3. Handling Type Conversion Errors

When performing explicit type conversions, it’s essential to handle potential errors gracefully. You can use try-catch blocks to catch FormatException or TypeError.

Example:

void main() { String strValue = 'not a number'; try { int intValue = int.parse(strValue); // This will throw an error } catch (e) { print('Error: $e'); // Output: Error: FormatException: Invalid number (at character 1) } }

4. Summary

  • Implicit Type Conversion: Dart automatically converts smaller types to larger types when safe to do so, such as int to double.
  • Explicit Type Conversion: You can manually convert types using as for casting, toString() for converting to strings, and parse() methods for converting strings to numbers.
  • Error Handling: When performing explicit conversions, especially from strings to numeric types, it’s essential to handle potential errors using try-catch.

Understanding type conversion in Dart allows you to work effectively with different data types and write robust code that can handle various scenarios.