toInt method

int toInt({
  1. dynamic def = -1,
})

Converts the string to an integer.

If the string cannot be parsed into an integer, the method returns the default value provided. The default value is -1 if not specified.

Example:

var numStr = '123';
var invalidStr = 'abc';
print(numStr.toInt()); // Outputs: 123
print(invalidStr.toInt()); // Outputs: -1

def The default value to return if the string cannot be parsed. Defaults to -1.

Returns the integer value of the string or the default value if parsing fails.

Implementation

int toInt({var def = -1}) {
  return int.tryParse(this) ?? def;
}