TypeConversion
Type Conversion in Dart
Type conversion allows us to convert one data type to another type. For example, We can convert from String
to int
, int
to String
, or String
to bool
, and so on.
Convert String to Int in Dart
To convert a String
to an int
in Dart, you can use the int.parse()
method. This method takes a String
as an argument and converts it into an integer.
Convert String to Double in Dart
To convert a String
to a double
in Dart, you can use the double.parse()
method. This method takes a String
as an argument and converts it into a double.
Convert int
to String
in Dart
int
to String
in DartIn Dart, you can convert an int
to a String
by using the toString()
method or by using string interpolation to implicitly convert it within a string.
Using toString()
toString()
In the example above, we have an int
variable intValue
with the value 42
. We then use the toString()
method to convert it into a String
, which is stored in the strValue
variable.
Using String Interpolation
In this example, we also have an int
variable intValue
with the value 42
. We use string interpolation by enclosing intValue
within double quotes ("$intValue"
) to implicitly convert it into a String
, which is stored in the strValue
variable.
Both approaches result in converting the int
to a String
. You can choose the one that suits your coding style and needs.
The code examples above will output:
Last updated