asCast<T> method

T? asCast<T>({
  1. T? def,
})

Implementation

T? asCast<T>({T? def}) {
  var value = this as Object?;
  if (value == null) {
    return def;
  }

  switch (T.toString()) {
    case 'int':
      return value.asInt(def: def as int) as T;
    case 'double':
      return value.asDouble(def: def as double) as T;
    case 'num':
      return value.asNum(def: def as num) as T;
    case 'String':
      return value.asString(def: def as String) as T;
    case 'bool':
      return value.asBool(def: def as bool) as T;
    case 'List':
      return value.asList(def: def as List) as T;
    case 'ObjectId':
      return value.asObjectId(def: def as ObjectId) as T;
    case 'DateTime':
      return value.asDateTime(def: def as DateTime) as T;
    case 'int?':
      int? defVal = (def == null ? null : def) as int?;
      return value.asInt(def: defVal) as T?;
    case 'double?':
      var defVal = (def == null ? null : def) as double?;
      return value.asDouble(def: defVal) as T?;
    case 'num?':
      var defVal = (def == null ? null : def) as num?;
      return value.asNum(def: defVal) as T?;
    case 'String?':
      var defVal = (def == null ? null : def) as String?;
      return value.asString(def: defVal) as T?;
    case 'bool?':
      var defVal = (def == null ? null : def) as bool?;
      return value.asBool(def: defVal) as T?;
    case 'List?':
      var defVal = (def == null ? null : def) as List?;
      return value.asList(def: defVal) as T?;
    case 'ObjectId?':
      var defVal = (def == null ? null : def) as ObjectId?;
      return value.asObjectId(def: defVal) as T?;
    case 'DateTime?':
      var defVal = (def == null ? null : def) as DateTime?;
      return value.asDateTime(def: defVal) as T?;
    default:
      return def;
  }
}