renameKey method

  1. @useResult
Map<K, V> renameKey(
  1. K oldKey,
  2. K newKey
)

New map with oldKey renamed to newKey; unchanged if oldKey absent. Audited: 2026-06-12 11:26 EDT

Implementation

@useResult
Map<K, V> renameKey(K oldKey, K newKey) {
  // Rebuild into a fresh map (preserves a genuinely-null value — the old
  // `if (v != null)` guard dropped entries whose value was null) and re-key
  // each entry in one pass so there is no remove-then-overwrite hazard.
  final Map<K, V> out = <K, V>{};
  forEach((K k, V v) {
    out[k == oldKey ? newKey : k] = v;
  });
  return out;
}