whereNotEmpty<K, V> static method

Map<K, V> whereNotEmpty<K, V>(
  1. Map<K, V> map
)

Returns a new map with entries whose values are null or empty (empty String, Iterable, or Map) removed.

Obj.whereNotEmpty({'name': 'Anna', 'tags': [], 'bio': ''});
// {'name': 'Anna'}

Implementation

static Map<K, V> whereNotEmpty<K, V>(Map<K, V> map) {
  bool empty(dynamic v) {
    if (v == null) return true;
    if (v is String) return v.isEmpty;
    if (v is Iterable) return v.isEmpty;
    if (v is Map) return v.isEmpty;
    return false;
  }

  return {
    for (final e in map.entries)
      if (!empty(e.value)) e.key: e.value,
  };
}