compact method

List<T> compact()

Remove null or empty elements from the list. Returns a new list with null or empty elements removed.

Implementation

List<T> compact() {
  final list = <T>[];
  for (final item in this) {
    if (item == null) {
      continue;
    } else if (item is String) {
      if (item.isNotEmpty) {
        list.add(item);
      }
    } else if (item is Iterable) {
      if (item.isNotEmpty) {
        list.add(item);
      }
    } else if (item is Map) {
      if (item.isNotEmpty) {
        list.add(item);
      }
    } else {
      list.add(item);
    }
  }
  return list;
}