asList<T> method

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

Implementation

List<T> asList<T>({List<T>? def}) {
  try {
    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) {
    return def ?? [];
  }
}