mu_state 0.3.0
mu_state: ^0.3.0 copied to clipboard
A set of helpers for pragmatic state handling as mentioned in my Medium article.
mu_state #
A minimal state solution based on my Pragmatic state handling in Flutter Medium article .
Features #
A set of classes built on ValueNotifier and ValueListenableBuilder.
MuEvent<T>- the base class for our 3 state objectMuEventData<T>- the data state of typeTMuEventError<T>- the error state of typeTMuEventLoading<T>- the loading state of typeTMuState<T>- aValueNotifierof typeMuEvent<T>MuBuilder<T>- aValueListenableBuilderof typeMuEvent<T>MuMultiBuilder- listen to a list ofMuState's and get notified with a list ofMuEvent's
How To #
Declare state as a global final MuState<T> variable and pass it an initial MuEvent
type, e.g. a MuEventLoading or a MuEventData<T>.
Alternatively extend MuState and implement the necessary methods.
Listen to MuState changes using MuBuilder.
Listen to multiple MuState objects using MuMultiBuilder and handle the list
of MuEvent's on changes.
Example #
In test_state.dart:
import 'package:mu_state/mu_state.dart';
class CounterState extends MuState<int> {
CounterState(MuEvent initValue) : super(initValue);
void increment() {
value = MuEventData((value as MuEventData<int>).data + 1);
}
}
final counterState = CounterState(const MuEventData(0));
In main.dart:
Scaffold(
body: Center(
child: MuBuilder(
state: counterState,
builder: (context, event, child) {
return switch (event) {
MuEventLoading() => const CircularProgressIndicator(),
MuEventError(error: Object error) => Text('Error: $error'),
MuEventData(data: int value) => Text('$value')
};
},
),
),
),
Also see Example.