Dart BigInt isValidInt
Syntax & Examples


BigInt.isValidInt property

The `isValidInt` property indicates whether the big integer can be represented as an `int` without losing precision.


Syntax of BigInt.isValidInt

The syntax of BigInt.isValidInt property is:

 bool isValidInt 

This isValidInt property of BigInt whether this big integer can be represented as an int without losing precision.

Return Type

BigInt.isValidInt returns value of type bool.



✐ Examples

1 Check if BigInt can be represented as int

In this example,

  1. We create two BigInts, bigInt1 with a large value and bigInt2 with a small value.
  2. We use the isValidInt property to check if the BigInts can be represented as int without losing precision.
  3. We print the result for both BigInts.

Dart Program

void main() {
  BigInt bigInt1 = BigInt.parse('12345678901234567890');
  BigInt bigInt2 = BigInt.from(123456789);
  print('BigInt 1 can be represented as int: ${bigInt1.isValidInt}');
  print('BigInt 2 can be represented as int: ${bigInt2.isValidInt}');
}

Output

BigInt 1 can be represented as int: false
BigInt 2 can be represented as int: true

2 Check if negative BigInt can be represented as int

In this example,

  1. We create two BigInts, bigInt1 with a large negative value and bigInt2 with a small negative value.
  2. We use the isValidInt property to check if the BigInts can be represented as int without losing precision.
  3. We print the result for both BigInts.

Dart Program

void main() {
  BigInt bigInt1 = BigInt.parse('-98765432109876543210');
  BigInt bigInt2 = BigInt.from(-123456789);
  print('BigInt 1 can be represented as int: ${bigInt1.isValidInt}');
  print('BigInt 2 can be represented as int: ${bigInt2.isValidInt}');
}

Output

BigInt 1 can be represented as int: false
BigInt 2 can be represented as int: true

Summary

In this Dart tutorial, we learned about isValidInt property of BigInt: the syntax and few working examples with output and detailed explanation for each example.