decodeValue<T> function

T decodeValue<T>(
  1. Object? value
)

Scalar decoder shared by generated model and record selections.

Implementation

T decodeValue<T>(Object? value) {
  if (value == null) throw FormatException('Unexpected SQL NULL for $T');
  if (T == DateTime) {
    if (value is DateTime) return value.toUtc() as T;
    if (value is int) {
      return DateTime.fromMicrosecondsSinceEpoch(value, isUtc: true) as T;
    }
  }
  if (value is T) return value as T;
  if (T == bool && value is int && (value == 0 || value == 1)) {
    return (value == 1) as T;
  }
  if (T == double && value is num) return value.toDouble() as T;
  if (T == Uint8List && value is List<int>) {
    return Uint8List.fromList(value) as T;
  }
  throw FormatException('Cannot decode ${value.runtimeType} as $T');
}