descovr static method

Object? descovr(
  1. Object? value,
  2. Type type, {
  3. Object? def,
})

Implementation

static Object? descovr(Object? value, Type type, {Object? def}) {
  // if (value == null && def == null) {
  //   return null;
  // }
  switch (type.toString()) {
    case 'int':
      return value.asInt(def: (def ?? 0) as int?);
    case 'double':
      return value.asDouble(def: (def ?? 0.0) as double);
    case 'bool':
      return value.asBool(def: (def ?? false) as bool);
    case 'DateTime':
      return value.asDateTime(def: (def ?? DateTime.utc(1977)) as DateTime);
    case 'String':
      return value.asString(def: (def ?? '') as String);
    case 'num':
      return value.asNum(def: (def ?? 0) as num);
    case 'ObjectId':
      return value.asObjectId(
          def: (def ?? mongo.ObjectId()) as mongo.ObjectId);
    case 'List<String>':
      return value.asList<String>(def: (def ?? <String>[]) as List<String>);
    case 'List<ObjectId>':
      return value.asList<mongo.ObjectId>(
          def: (def ?? <mongo.ObjectId>[]) as List<mongo.ObjectId>);

    /// Nulables
    case 'int?':
      if (value == null) return def;
      return value.asInt(def: def as int?);
    case 'double?':
      if (value == null) return def;
      return value.asDouble(def: def as double?);
    case 'bool?':
      if (value == null && def == null) return null;
      return value.asBool(def: def as bool?);
    case 'DateTime?':
      if (value == null) return def;
      return value.asDateTime(def: def as DateTime?);
    case 'String?':
      if (value == null) return def;
      return value.asString(def: def as String?);
    case 'num?':
      if (value == null) return def;
      return value.asNum(def: def as num?);
    case 'ObjectId?':
      if (value == null) return def;
      return value.asObjectId(def: def as mongo.ObjectId?);
    case 'List<String>?':
      if (value == null) return def;
      return value.asList<String>(def: def as List<String>?);
    case 'List<ObjectId>?':
      if (value == null) return def;
      return value.asList<mongo.ObjectId>(def: def as List<mongo.ObjectId>?);
  }

  return (value ?? def);
}