intNullableConvert function

int? intNullableConvert(
  1. dynamic source
)

Converts dynamic value to int?.

Implementation

int? intNullableConvert(dynamic source) {
  if (source == null) return null;
  if (source is int) return source;
  if (source is double) {
    return source.isNaN || source.isInfinite ? null : source.toInt();
  }
  if (source is bool) return source ? 1 : 0;
  if (source is String) {
    return int.tryParse(source) ?? double.tryParse(source)?.toInt();
  }
  return null;
}