The BigInt static parse() method parses the given string source as a, possibly signed, integer literal and returns its value.
The syntax of BigInt.tryParse() static-method is:
BigInt tryParse(String source, { int radix })
This tryParse() static-method of BigInt parses source
as a, possibly signed, integer literal and returns its value.
Parameter | Optional/Required | Description |
---|---|---|
source | required | the string to attempt parsing as an integer |
radix | optional | the base of the numeral system used in source (defaults to 10 if not provided) |
BigInt.tryParse() returns value of type BigInt
.
In this example,
numberStr
with the value '12345'.tryParse()
method to attempt parsing the string as an integer and store the result in a nullable BigInt variable number
.number
is not null, then print the parsed number to standard output. If parsing fails, we print 'Invalid input'.void main() {
String numberStr = '12345';
BigInt? number = BigInt.tryParse(numberStr);
if (number != null) {
print('Parsed number: $number');
} else {
print('Invalid input');
}
}
Parsed number: 12345
In this example,
charStr
with the value 'A'.tryParse()
method with a radix of 16 (hexadecimal) to attempt parsing the string as a hexadecimal integer and store the result in a nullable BigInt variable charCode
.charCode
is not null, then print the parsed character code to standard output. If parsing fails, we print 'Invalid input'.void main() {
String charStr = 'A';
BigInt? charCode = BigInt.tryParse(charStr, radix: 16);
if (charCode != null) {
print('Parsed character code: $charCode');
} else {
print('Invalid input');
}
}
Parsed character code: 10
In this example,
binaryStr
with the value '1010'.tryParse()
method with a radix of 2 (binary) to attempt parsing the string as a binary integer and store the result in a nullable BigInt variable decimalValue
.decimalValue
is not null, then print the parsed binary number to decimal form to standard output. If parsing fails, we print 'Invalid input'.void main() {
String binaryStr = '1010';
BigInt? decimalValue = BigInt.tryParse(binaryStr, radix: 2);
if (decimalValue != null) {
print('Parsed binary to decimal: $decimalValue');
} else {
print('Invalid input');
}
}
Parsed binary to decimal: 10
In this Dart tutorial, we learned about tryParse() static-method of BigInt: the syntax and few working examples with output and detailed explanation for each example.