Flutter Convert String Variable to Integer

Changing over String into Number is essential for nearly language. Dart is no special case. In this instructional exercise, we’re going to see approaches to parse a String into a number (int and twofold) utilizing parse() and tryParse() strategy. You likewise realize how to get Exception for invalid information string, or convert a Hex string to int in Dart/Flutter.

Parse a String into an int in Dart/Flutter

Utilizing parse() technique

To parse a string into an int, we use int class parse() technique:

var n = int.parse('42');
// 42

The info String could be marked (- or +):

var n1 = int.parse('-42');
// -42

var n2 = int.parse('+42');
// 42

Radix – Hex string to int

Now and again we need to work with string in radix number organization. Dart int parse() technique additionally bolsters convert string into a number with radix in the reach 2..36:

For instance, we convert a Hex string into int:

var n_16 = int.parse('FF', radix: 16);
// 255

parse() strategy and catch Exception

Whenever input string isn’t in a substantial number structure, the program tosses a FormatException:

var n = int.parse('n42');
/*
Exception has occurred.
FormatException (FormatException: Invalid radix-10 number (at character 1)
n42
^
*/

So to deal with this case, we can utilize Dart attempt get block:

try {
  var n = int.parse('n42');
  print(n);
} on FormatException {
  print('Format error!');
}
// Format error!

int class parse() technique likewise gives us an approach to deal with FormatException case with onError boundary.

var num4 = int.parse("n42", onError: (source) => -1);
// -1

At the point when the exemption is tossed, onError will be called with source as info string. Presently we can restore a whole number worth or invalid… In the model over, the capacity returns – 1 at whatever point source is in wrong whole number exacting.

Utilizing tryParse() strategy

Rather than int.parse(string, onError: (string) => ...), we should use int.tryParse(string):

var n0 = int.tryParse('42');
// 42

var n1 = int.tryParse('-42');
// -42

var n2 = int.tryParse('FF', radix: 16);
// 255

What is the diverse among parse() and tryParse()?

  • parse() tosses a FormatException for invalid info string
  • tryParse() returns invalid for invalid info string

tryParse() strategy and Exception

For tryParse() strategy, we don’t need to get Exception, we just need to check invalid worth.

The assertion will be easier:

var n = int.tryParse('n42') ?? -1;
// -1

We can likewise restore a string esteem while parse() technique with onError boundary just brings number back:

var n = int.tryParse('n42') ?? 'Format error!';
// 'Format error!'

Parse a String into a twofold in Dart/Flutter

Utilizing parse() strategy

Like whole number, string in twofold strict can be parsed into twofold utilizing parse() technique:

var n1 = double.parse('4.2');
// 4.2

var n2 = double.parse('0.');
// 0.0

var n3 = double.parse('.0');
// 0.0

var n4 = double.parse('-1.e3');
// -1000.0

var n5 = double.parse('123E+3');
// 123000.0

var n6 = double.parse('+.12e-2');
// 0.0012

var n7 = double.parse('+.12e-6');
// 1.2e-7

var n8 = double.parse('-NaN');
// NaN

parse() strategy and catch Exception

Whenever input string isn’t in a substantial twofold structure, the program tosses a FormatException:


var n = double.parse('n4.2');
/*
Exception has occurred.
FormatException (FormatException: Invalid double n4.2)
*/

We can utilize Dart attempt get square to deal with this Exception:

try {
  var n = double.parse('n4.2');
  print(n);
} on FormatException {
  print('Format error!');
}
// Format error!

Dart twofold class parse() strategy can deal with FormatException case with extra onError boundary.

var n = double.parse('n4.2', (source) => -1);
// -1.0

Utilizing tryParse() technique

Rather than tossing FormatException for invalid information string, twofold class tryParse() technique brings invalid back:

double n = double.tryParse('n4.2');
// null

Utilizing tryParse() strategy, we can check invalid at that point return explicit worth.

double n = double.tryParse('n4.2') ?? -1;
// -1.0

Conclusion

We’ve known how to parse a String into a number (int or double) in Dart/Flutter by using parse() or tryParse() method.

Remember that onError parameter is deprecated and will be removed in the future. So tryParse() method could be the better choice.

Happy learning! See you again!

Leave a Comment