toValueOf<T extends Object> function

T toValueOf<T extends Object>(
  1. Object? data
)

Converts data to a value of T or throws FormatException if cannot.

Supported types for T: String, num, int, BigInt, double, bool, DateTime, Identifier, Object.

Implementation

T toValueOf<T extends Object>(Object? data) {
  if (data == null) {
    throw const NullValueException();
  } else if (T == String) {
    return toStringValue(data) as T;
  } else if (T == num) {
    return toNumValue(data) as T;
  } else if (T == int) {
    return toIntValue(data) as T;
  } else if (T == BigInt) {
    return toBigIntValue(data) as T;
  } else if (T == double) {
    return toDoubleValue(data) as T;
  } else if (T == bool) {
    return toBoolValue(data) as T;
  } else if (T == DateTime) {
    return toTimeUTCValue(data) as T;
  } else if (T == Identifier) {
    return toIdValue(data) as T;
  } else if (T == Object) {
    return data as T;
  }
  throw ConversionException(data: data, target: T);
}