parseInt function

int? parseInt(
  1. dynamic o, [
  2. int? def
])

Parses o as int. If can't parse returns def.

Implementation

int? parseInt(dynamic o, [int? def]) {
  if (o == null) return def;
  if (o is int) return o;
  if (o is num) return o.toInt();

  var s = o.toString().trim();

  var d = int.tryParse(s);
  d ??= double.tryParse(s)?.toInt();

  return d ?? def;
}