toInt function

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

Converts an object to an int. mirroring the same static method in the ConvertObject, providing alternative easy less code usage options.

  • Converts numeric types and strings that represent valid integers to int.
  • If the conversion to int fails (e.g., non-integer string), throws a ParsingException.

object The object to be converted to an int. 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 an int if conversion is successful. Throws a ParsingException if the conversion fails.

Example usage:

final object1 = 10;
final int1 = toInt(object1); // 10

final object2 = '5';
final int2 = toInt(object2); // 5

final object3 = true;
final int3 = toInt(object3); // 1

final object4 = 'abc';
final int4 = toInt(object4); // throws ParsingException

Implementation

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