millisecondsDurationFromJson static method

Duration? millisecondsDurationFromJson(
  1. dynamic value
)

Converts a value of milliseconds to a Duration object.

  • double => floor of value, passed to Duration
  • integer => value passed to Duration
  • string => value parsed as int, passed to Duration
  • boolean => null

Implementation

static Duration? millisecondsDurationFromJson(dynamic value) {
    try {
        switch(value.runtimeType) {
            case bool: return null;
            case int: return Duration(milliseconds: (value as int));
            case double: return Duration(milliseconds: (value as double).floor());
            case String: return Duration(milliseconds: int.parse(value as String));
            default: return null;
        }
    } catch(_) {};
    return null;
}