toIntSafe method

int? toIntSafe()

Converts this string to an int, removing commas and other formatting first.

Returns null if the string cannot be parsed as an integer.

Example:

String text = '11,111';
int? value = text.toIntSafe(); // 11111

Implementation

int? toIntSafe() {
  final cleaned = numbersOnly;
  if (cleaned.isEmpty) return null;
  return int.tryParse(cleaned);
}