toNullableLong static method

int? toNullableLong(
  1. dynamic value
)

Converts value into long or returns null when conversion is not possible.

  • value the value to convert. Returns long value or null when conversion is not supported.

Implementation

static int? toNullableLong(value) {
  if (value == null) return null;
  if (value is int) return value;
  if (value is double) return value.ceil();
  if (value is DateTime) return value.millisecondsSinceEpoch;
  if (value is Duration) return value.inMilliseconds;
  if (value is bool) return value ? 1 : 0;

  var result = double.tryParse(value.toString());
  return result == null ? null : result.truncate();
}