parseValueForType<V> static method

V? parseValueForType<V>(
  1. Type type,
  2. Object? value, [
  3. V? def
])

Parses value using a parserFor type. Returns value if can't parse.

Implementation

static V? parseValueForType<V>(Type type, Object? value, [V? def]) {
  if (value == null) return def;

  var typeInfo = TypeInfo.from(type);
  var valueTypeInfo = TypeInfo.from(value);

  if (valueTypeInfo == typeInfo) {
    return value as V;
  }

  var parser = parserFor(typeInfo: typeInfo);

  if (parser != null) {
    var parsed = parser(value);
    return parsed ?? def;
  } else {
    return def;
  }
}