omitNull function

Map<String, Object>? omitNull(
  1. Map<String?, Object?> map
)

Returns a new map with all null entries removed from the given map.

If the resulting map is empty, this function returns null.

map: The map to remove null entries from.

Implementation

Map<String, Object>? omitNull(Map<String?, Object?> map) {
  var result = <String, Object>{};
  map.forEach((key, value) {
    if (key != null && value != null) {
      result[key] = value;
    }
  });
  if (result.isEmpty) {
    return null;
  }
  return result;
}