tryParseInt function

int? tryParseInt(
  1. dynamic value
)

Attempts to parse a dynamic value to an int.

This function tries to convert the provided value to an int. If the value cannot be parsed, it returns null.

  • Parameters:

    • value: A dynamic value that is expected to be convertible to an int.
  • Returns: An int if the parsing is successful, or null if parsing fails.

  • Example:

tryParseInt('123');       // returns 123
tryParseInt('abc');       // returns null
tryParseInt(42);          // returns 42 (automatic conversion from int)
tryParseInt(null);        // returns null

Implementation

int? tryParseInt(dynamic value) {
  return int.tryParse(value.toString());
}