fromValue<T> static method

T fromValue<T>(
  1. dynamic value
)

Implementation

static T fromValue<T>(dynamic value) {
  if (value.runtimeType == T || value == null) {
    return value as T;
  } else {
    TypeInfo typeInfo;
    if (value is Map<String, dynamic> && value['__type'] != null) {
      typeInfo = TypeInfo.fromType(value['__type'] as String);
    } else {
      typeInfo = TypeInfo.fromType<T>();
    }
    var mapper = _mappers[typeInfo.type];
    if (mapper?.decoder != null) {
      try {
        return genericCall(typeInfo, mapper!.decoder!, value) as T;
      } catch (e) {
        throw MapperException('Error on decoding type $T: ${e is MapperException ? e.message : e}');
      }
    } else {
      throw MapperException('Cannot decode value $value of type ${value.runtimeType} to type $T. Unknown type. Did you forgot to include the class or register a custom mapper?');
    }
  }
}