asList<T> method

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

Implementation

List<T> asList<T>({List<T>? def}) {
  try {
    if (T == ObjectId) {
      final res = <ObjectId>[];

      for (var e in this) {
        if (e is ObjectId) {
          res.add(e);
        } else if (e is String) {
          ObjectId? oid = e.toString().oID;
          if (oid != null) {
            res.add(oid);
          }
        }
      }

      return res as List<T>;
    }
    if (this is List) {
      return List<T>.from(this.map((x) => x));
    }

    if (this is String) {
      var res = (this.toString()).split(',').map((e) {
        if (T == String) {
          return e.toString().trim() as T;
        }
        return e as T;
      }).toList();
      res.removeWhere((element) => element == null || element == '');
      return res;
    }

    if (this is T) {
      return [this as T];
    }
    return def ?? [];
  } catch (e) {
    Console.e(e);
    return def ?? [];
  }
}