Tiny predictable opinionated state management package with prebuilt concurrency handlers that implement the Business Logic Component design pattern without complexity of flutter_bloc.
- Concurrency: supports variative and expandable concurrency handlers (droppable, throttled, debounced, concurrent, restartable) and gives you possibility to create your own concurrency handler
- No third-part dependencies: based on Flutter's
ChangeNotifier
,Stream
andInheritedNotifier
for scoping - Can be listened in various ways:
ListenableBuilder
,InheritedWidget/InheritedNotifier
,StreamBuilder
, etc.. - Ready to complex state managment: can handle many various cases like auth, pagination, notifications, complex loading and state handling
flutter pub add streamed_controller
- Define controller class (and state class, if you using streamed_controller for complex state managment):
class TestControllerBase extends StreamedController<int> {
TestControllerBase() : super(initialState: 0, eventHandler: ConcurrentConcurrencyHandler());
Future<void> incrementAwaitable() => handle(() async* {
await Future.delayed(const Duration(seconds: 1));
yield state+1;
}());
}
- Create controller variable in any place where you ready to store it (Stateful Scopes, Singletons or other storages):
final $controller = TestControllerBase();
- Wrap widget which will reacts to controller changes into ListenableBuilder/AnimatedBuilder:
ListenableBuilder(
listenable: $controller,
builder: (context, child) => Text(
'${$controller.state}',
style: Theme.of(context).textTheme.headlineMedium,
))
- And then call these functions from your code
FloatingActionButton(
onPressed: $controller.increment,
tooltip: 'Async increment',
child: const Icon(Icons.add),
)
To create your own controller observer, that watches for all controller's lifecycles, you need to extend StreamedControllerObserver and override its methods:
onCreate
onDispose
onError
onStateChanged
Then you need to assign a static variable observer
to your observer object in
the initialization (or other place) of your code
StreamedController.observer = MyCoolObserver();
By default, you can try logger that uses internal dart:developer
log
method
Pass it like this:
StreamedController.observer = StreamedControllerObserver.dartLog();
StreamedControllerObserver example:
class _TestControllerObserver extends StreamedControllerObserver {
@override
void onCreate(StreamedController<Object> controller) =>
log('${controller.runtimeType} created');
@override
void onDispose(StreamedController<Object> controller) =>
log('${controller.runtimeType} disposed');
@override
void onError(StreamedController<Object>? controller, Object? error,
StackTrace stackTrace) =>
log('${controller.runtimeType} got error: $error');
@override
void onStateChanged(StreamedController<Object> controller, Object prevState,
Object nextState) =>
log('${controller.runtimeType} state updated, new state: $nextState');
}
StreamedController
exposes stateChanges
lazy Stream<State>
that emits new
state to it listeners, basing on Stream API In general, you won't have to use
this stream for rebuilding states or etc, because you have all features from
ChangeNotifier
API
stateChanges
stream is lazy, which means it doesn't create until first call
2023, Archie Kitsuniru (kitsuniru)