How to Convert the String to an Int in Dart
In this tutorial, we are going to learn about how to convert the string to an integer (Int) in Dart with the help of examples.
Consider, we have the following string in our code:
var a = "21"
Now, we need to convert the above string "21"
to an integer 21
.
Using int.parse() method
To convert a string to an integer, we can use the built-in int.parse()
method in Dart.
The int.parse()
function takes the string as an argument and converts it to the integer.
Here is an example, that converts the string "21"
to an integer:
void main() {
var a = "21";
var num = int.parse(a); // converts string to int
print(num);
}
Output:
21
Similarly, we can also convert the string to double by using the double.parse()
method in Dart.
void main() {
var a = "100.343";
var result = double.parse(a); // converts string to double
print(result);
}
Output:
100.343