retainKeys method

Map<T, T> retainKeys(
  1. List<T> keys
)

Removes the key/value pairs from the map whose keys are not present in the given keys list.

Returns a new map containing only the key/value pairs whose keys are present in the given keys list.

Example:

Map<String, dynamic> map = {"id": 1, "name": "John", "age": 30};
var newMap = map.retainKeys(["id", "name"]);
print(newMap); // {"id": 1, "name": "John"}

Implementation

Map<T, T> retainKeys(List<T> keys) {
  removeWhere((T key, T value) => !keys.contains(key));
  return this;
}