toNullableType<T> static method

T? toNullableType<T>(
  1. TypeCode? type,
  2. dynamic value
)

Converts value into an object type specified by Type Code or returns null when conversion is not possible.

  • type the TypeCode for the data type into which 'value' is to be converted.
  • value the value to convert. Returns object value of type corresponding to TypeCode, or null when conversion is not supported.

See toTypeCode

Implementation

static T? toNullableType<T>(TypeCode? type, value) {
  if (value == null) return null;

  // Convert to known types
  if (type == TypeCode.String) {
    value = StringConverter.toNullableString(value);
  } else if (type == TypeCode.Boolean) {
    value = BooleanConverter.toNullableBoolean(value);
  } else if (type == TypeCode.Integer) {
    value = IntegerConverter.toNullableInteger(value);
  } else if (type == TypeCode.Long) {
    value = LongConverter.toNullableLong(value);
  } else if (type == TypeCode.Float) {
    value = FloatConverter.toNullableFloat(value);
  } else if (type == TypeCode.Double) {
    value = DoubleConverter.toNullableDouble(value);
  } else if (type == TypeCode.DateTime) {
    value = DateTimeConverter.toNullableDateTime(value);
  } else if (type == TypeCode.Duration) {
    value = DurationConverter.toNullableDuration(value);
  } else if (type == TypeCode.Array) {
    value = ArrayConverter.toNullableArray(value);
  } else if (type == TypeCode.Map) value = MapConverter.toNullableMap(value);

  return value;
}