putAllAndUpdateExisting method

  1. @override
Future<void> putAllAndUpdateExisting(
  1. Map<TKey, TVal> newValues,
  2. void mutateExisting(
    1. TKey key,
    2. TVal mutateMe,
    3. TVal newValueReadOnly
    )
)
override

Same as putAll, but instead of removing old objects, changes (mutates) their values to new values

Implementation

@override
Future<void> putAllAndUpdateExisting(
  Map<TKey, TVal> newValues,
  void Function(TKey key, TVal mutateMe, TVal newValueReadOnly)
      mutateExisting,
) async {
  final actualNewValue = <TKey, TVal>{};
  for (var item in newValues.entries) {
    final key = item.key;
    final val = item.value;

    if (!dataBox.containsKey(key)) {
      actualNewValue[key] = val;
    } else {
      final oldVal = await getValueById(key);
      mutateExisting(key, oldVal!, val);
      actualNewValue[key] = oldVal;
    }
  }
  await putAll(actualNewValue);
}