tryToBigInt function

BigInt? tryToBigInt(
  1. dynamic object, {
  2. Object? mapKey,
  3. int? listIndex,
})

Attempts to convert an object to a BigInt, or returns null if the object is null or conversion fails. mirroring the same static method in the ConvertObject, providing alternative easy less code usage options.

  • Converts numeric types and strings that represent valid large integers to BigInt.
  • IMPORTANT: BigInt operations can be computationally expensive, especially for very large integers. Use BigInt only when necessary, and be mindful of performance implications.
  • If the conversion to BigInt fails (e.g., non-numeric string), logs an error and returns null.

object The object to be converted to a BigInt. mapKey (Optional) Specifies the key to extract values from a Map object. listIndex (Optional) Specifies the index to extract elements from a List object.

Returns a BigInt if conversion is successful, otherwise null.

Example usage:

final object1 = 10;
final bigInt1 = tryToBigInt(object1); // BigInt.from(10)

final object2 = '1234567890';
final bigInt2 = tryToBigInt(object2); // BigInt.from(1234567890)

final object3 = 'abc';
final bigInt3 = tryToBigInt(object3); // null

final object4 = null;
final bigInt4 = tryToBigInt(object4); // null

Implementation

BigInt? tryToBigInt(
  dynamic object, {
  Object? mapKey,
  int? listIndex,
}) =>
    ConvertObject.tryToBigInt(object, mapKey: mapKey, listIndex: listIndex);