How To Easily Convert Flutter String To Int – Easy Flutter Guide

flutter string to int

In this Flutter post, we’ll learn how to convert Flutter string to int by using an easy but proper multiple Flutter examples. We’ll be explaining this conversion step by step for better understanding.

After reading this post, I hope you’ll be able to easily implement this conversion in your own Flutter code.

So let’s get right into its practical implementation.

What is Flutter String To Int?

It defines the process to convert string to integer in Flutter. For instance, we have a text ’24’. Although, it looks like an integer but its not. Its datatype is string. We’ll see how to convert this string to integer and would also see other example for more detailed knowledge.

So let’s get right into implementing this Flutter string to int conversion.

Converting Flutter String To Int (Multiple Easy Examples)

For that, we have to use the int.parse method. See below syntax:

int.parse(string to be converted)

We have to pass our Flutter string to that method in order for it to get converted to Flutter integer. See below examples.

Example 1: Converting String To Integer

 String value = '454';
 print( value.runtimeType );
 var newVal = int.parse( value );
 print( newVal.runtimeType );

Output:

String
int
  • We have first created and initialized a string. Then we’ve printed its data type which shows string.
  • After that, we have used the int.parse method and passed it our string variable. It has converted that variable from Flutter string to int.
  • We also have created another variable and passed the converted value to it.
  • Finally, we have printed its type and its showing int which means we have successfully implemented our Flutter string to int conversion.
Let’s now try doing addition with that value to make sure its really converted to integer. See below code:
String value='454';
var newVal= int.parse(value);
int addedVal= 23 + newVal;
print(addedVal);

Output:

477

You can see that its working perfectly fine.

Example 2: Accept String Starting From 0x(zero and x)

The int.parse method accepts string that starts from 0x(zero and x) which is a hexadecimal number format. See below example:
String value='0x1e';           // hex code for 30
var newVal= int.parse(value);
int addedVal= 23 + newVal;
print(addedVal);

Output:

53

So this is how you can easily convert Flutter string to int.

Conclusion

To conclude, hope you now have a clear in-depth knowledge of how to properly convert Flutter string to int. Your feedback would be well appreciated. Thank you for reading this post.

Leave a Comment

Your email address will not be published. Required fields are marked *