updateByKeySafe method

  1. @nonVirtual
F updateByKeySafe(
  1. E key,
  2. dynamic value
)

Calls updateByKey and validates the result in debug mode.

In debug builds, asserts that getValueByKey on the returned instance equals value. A mismatch means either updateByKey or getValueByKey is incorrectly implemented.

In release builds the assertion is stripped — no runtime overhead.

Implementation

@nonVirtual
F updateByKeySafe(E key, dynamic value) {
  final result = updateByKey(key, value);

  assert(() {
    final setValue = result.getValueByKey(key);

    const equality = DeepCollectionEquality();

    if (!equality.equals(setValue, value)) {
      throw Exception(
        'Failed to update key $key with value $value.\n'
        'Either "updateByKey" or "getValueByKey" is incorrectly implemented '
        'in the subclass. Expected "$value" but got "$setValue".',
      );
    }

    return true;
  }());

  return result;
}