orbital_state 0.2.1
orbital_state: ^0.2.1 copied to clipboard
Reactive state mixin and UI helpers for Orbital lifecycle objects.
orbital_state #
Reactive state mixin for OrbitalLifecycle objects in the Orbital ecosystem.
What this package contains #
orbital_state provides StateMixin<T>, a small mixin that adds reactive
state to any OrbitalLifecycle controller (as used by orbital_injector and
orbital_router), without requiring an external state manager.
It handles:
- lazily creating a
ValueNotifier<T>on the firstemit - rebuilding widgets via
watchwhenever state changes - subscribing other lifecycle owners via
listen, with automatic cleanup throughaddDisposerwhen the owner is disposed - disposing the underlying notifier from
onDispose
The framework does not define loading/error/empty states — model T as you
need (sealed class, enum, plain class, etc.).
Quick start #
class CounterController extends OrbitalLifecycle with StateMixin<int> {
@override
FutureOr<void> onInit() {
emit(0);
}
void increment() => emit(state + 1);
}
// In a widget:
controller.watch((count) => Text('count:$count'));
watch vs listen #
watchreturns a widget that rebuilds itself wheneveremitchanges state.listensubscribes anOrbitalLifecycleowner to another controller's state changes with a plain callback, and automatically removes the subscription when the owner is disposed. PassfireImmediately: trueto invoke the callback once with the current state when registering.
Both require at least one prior emit — emit the initial state in onInit
before calling watch or listen.
More detail #
See doc.md for the design rationale and lifecycle interaction with
orbital_core and orbital_injector.