derivedSetting<T, I> method
Setting<T>
derivedSetting<T, I>({
- required String key,
- required ReadableBeacon<
I> input, - required T decode(),
- required RawSettingValue encode(
- T
A Setting that is derived from input. Any time the input
changes, the decode function will be invoked with the current
value of the returned Setting as well as the new value of input.
Similarly, any time the returned Setting changes, the decode function
will be invoked with the new value of the returned Setting as well as
the current value of input.
Implementation
Setting<T> derivedSetting<T, I>({
required String key,
required ReadableBeacon<I> input,
required T Function(RawSettingValue, I) decode,
required RawSettingValue Function(T) encode,
}) {
final s = setting<T>(
key: key,
decode: (value) => decode(value, input.value),
encode: (value) => encode(value),
);
_group.effect(
() {
final $input = input.value;
final $s = s.value.value;
final decoded = decode(s._toRawValue(_storage.get(key)), $input);
if (decoded != $s) {
s.value.value = decoded;
}
},
name: 'derivedSettingEffect',
);
return s;
}