updateDeeply function

dynamic updateDeeply(
  1. List keyPath,
  2. dynamic data,
  3. Function updater, [
  4. dynamic notSetValue,
  5. int i = 0,
])

set at keyPath of data the value of updater-function with value of data at keyPath or notSetValue as argument

Implementation

dynamic updateDeeply(List keyPath, dynamic data, Function updater,
    [dynamic notSetValue, int i = 0]) {
  if (i == keyPath.length) {
    return updater(data == null ? notSetValue : data);
  }

  if (!(data is Map)) {
    data = {};
  }

  data = Map<dynamic, dynamic>.from(data);

  data[keyPath[i]] =
      updateDeeply(keyPath, data[keyPath[i]], updater, notSetValue, ++i);

  return data;
}