This is several questions about int.parse in Dart...
I know that in Dart we can parse a string as an int and catch exceptions using something like:
try {
n = int.parse(input.value);
// etc.
} on FormatException {
// etc.
}
(Which is fine.)
In the documentation, there is the following description of int.parse:
int parse(String source, int radix, int onError(String source))
When I tried using int.parse with more than one argument, however, I got a complaint from the editor about me using extra arguments. Am I misunderstanding the documentation? And how does one, for example, set the radix?
In Dart 2, int.tryParse is available.
It returns null for invalid inputs instead of throwing. You can use it like this:
int val = int.tryParse(text) ?? defaultValue;
The onError
parameter in int.parse
is deprecated.
Int.parse
uses named, optional parameters.
API:
int parse(String source, {int radix, int onError(String source)})
The { }
around params in the parameter list indicate that these are optional, named parameters.
(If you had [ ]
around params in the parameter list, these would be optional, positional parameters)
Example Usage:
int.parse("123");
int.parse("123", radix:16);
int.parse("123", onError:(source) => print("Source"));
int.parse("123", radix:16, onError:(source) => print(source));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With