convertFrom<T> method

  1. @override
T? convertFrom<T>(
  1. dynamic value
)
override

Converter for converting value to type T.

Implementation

@override
T? convertFrom<T>(dynamic value) {
  if (value == null) {
    return null;
  }
  switch (T) {
    case DateTime:
      if (value is num) {
        return DateTime.fromMillisecondsSinceEpoch(value.toInt()) as T;
      } else if (value is String) {
        return DateTime.parse(value) as T;
      }
      throw Exception(
        "Could not convert ${value.runtimeType} to ${T.toString()}.",
      );
    case int:
      if (value is int) {
        return value.toInt() as T;
      } else if (value is String) {
        return int.parse(value) as T;
      }
      throw Exception(
        "Could not convert ${value.runtimeType} to ${T.toString()}.",
      );
    case double:
      if (value is num) {
        return value.toDouble() as T;
      } else if (value is String) {
        return double.parse(value) as T;
      }
      throw Exception(
        "Could not convert ${value.runtimeType} to ${T.toString()}.",
      );
    case String:
      return value.toString() as T;
    default:
      return value as T;
  }
}