value property
Returns the current value and establishes a reactive dependency.
When accessed within a reactive context (like a Computed or Effect), the context will be notified when this signal changes.
Example:
final counter = Signal(0);
final doubled = Computed(() => counter.value * 2); // Creates dependency
Implementation
@override
T get value {
// Trigger lazy initialization
if (!_isInitialized && _initCompleter == null) {
unawaited(_load());
}
return super.value;
}
Sets a new value for the signal and notifies subscribers when it changes.
Parameters:
- value: The new value to set
Example:
final counter = Signal(0);
counter.value = 10;
counter.set(11);
Sets a new value for the signal and notifies subscribers when it changes.
Parameters:
- value: The new value to set
Example:
final counter = Signal(0);
counter.value = 10;
counter.set(11);
Implementation
@override
set value(T newValue) {
// Enforce initialization
if (!_isInitialized) {
throw StateError(
'Cannot write to PersistSignal before initialization completes. '
'Use await signal.getEnsured() or await signal.ensure() first.',
);
}
// Update value immediately (optimistic)
super.value = newValue;
_version++;
// Schedule write
_scheduleWrite(newValue);
}