updateByKeySafe method
- @nonVirtual
- E key,
- dynamic value
A safe version of updateByKey that performs a debug-mode consistency check.
This method:
- Calls the subclass's updateByKey implementation to produce a new updated instance.
- In debug mode (
assertenabled), verifies that the updated instance actually contains the expected value by calling getValueByKey.
If a mismatch is detected, an exception is thrown to warn developers that
either updateByKey or getValueByKey is incorrectly implemented.
In release mode, the assertion block is removed entirely, so no runtime overhead or validation occurs.
Implementation
@nonVirtual
F updateByKeySafe(E key, dynamic value) {
// Perform the actual update.
final result = updateByKey(key, value);
// Debug-only validation to ensure correctness of subclass implementations.
assert(() {
final setValue = result.getValueByKey(key);
if (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;
}