mu_state 0.2.0 mu_state: ^0.2.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
- the base class for our 3 state objectMuEventData
- the data stateMuEventError
- the error stateMuEventLoading
- the loading stateMuState
- aValueNotifier
of typeMuEvent
MuBuilder
- aValueListenableBuilder
of typeMuEvent
MuMultiBuilder
- listen to a list ofMuState
's and get notified with a list ofMuEvent
's
How To #
Declare state as a global final MuState
variable and pass it an initial MuEvent
type, e.g. a MuEventLoading
or a MuEventData
.
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 {
CounterState(MuEvent initValue) : super(initValue);
void increment() {
value = MuEventData((value as MuEventData<int>).data + 1);
}
}
final counterState = CounterState(const MuEventData<int>(0));
In main.dart
:
Scaffold(
body: Center(
child: MuBuilder(
state: counterState,
builder: (context, event, child) {
return switch (event) {
MuEventLoading _ => const CircularProgressIndicator(),
MuEventError ev => Text('Error: ${ev.error}'),
MuEventData ev => Text('${ev.data}')
};
},
),
),
),
Also see Example.