How To Convert String Into Integer In Flutter?

In flutter, you can convert the string data type into an integer through int.parse() method. It accepts a string as an argument and converts it into an integer. In this article, we will learn it by an example. Initialize a variable with a string value with the var keyword.

var cricketPlayers = "11";

Before type casting, let’s check the current value and data type. You can check the data type of a variable through runtimeType property that finds the data type.

print(cricketPlayers);
// Output: 11
print(cricketPlayers.runtimeType);
// Output: String

Above we printed the stored value and its data type which is a string. Now we need to convert it into an integer through the int.parse() method. Pass the cricketPlayers variable into int.parse() method and assign it to a new variable. In this way, the new variable will be storing the int.parse() result.

// convert string into an integer
var convertIntoInteger = int.parse(cricketPlayers);

Above, the convertIntoInteger variable stores returned value from int.parse() method. Let’s check if the data type is converted.

print(convertIntoInteger);
// Output: 11
print(convertIntoInteger.runtimeType)
// Output: int

As you can see, the output is int which proves that the variable value is converted into an integer.

Conclusion

To convert a string value into an integer you can use int.parse() method that accepts string value and converts it into an integer.


Related Posts