toValue static method

dynamic toValue(
  1. dynamic value
)

Implementation

static dynamic toValue(dynamic value) {
  if (value == null) return null;
  var typeInfo = TypeInfo.fromValue(value);
  var mapper = _mappers[typeInfo.type] ?? _mappers.values
    .cast<BaseMapper?>()
    .firstWhere((m) => m!.isFor(value), orElse: () => null);
  if (mapper != null && mapper.encoder != null) {
    var encoded = mapper.encoder!.call(value);
    if (encoded is Map<String, dynamic>) {
      _clearType(encoded);
      if (typeInfo.params.isNotEmpty) {
        typeInfo.type = _typeOf(mapper.type);
        encoded['__type'] = typeInfo.toString();
      }
    }
    return encoded;
  } else {
    throw MapperException('Cannot encode value $value of type ${value.runtimeType}. Unknown type. Did you forgot to include the class or register a custom mapper?');
  }
}