https://github.com/erf/mu_state
A set of helpers for pragmatic state handling as mentioned in my Medium article
https://github.com/erf/mu_state
dart flutter flutter-state-management state state-management valuelistenablebuilder valuenotifier
Last synced: 3 months ago
JSON representation
A set of helpers for pragmatic state handling as mentioned in my Medium article
- Host: GitHub
- URL: https://github.com/erf/mu_state
- Owner: erf
- License: mit
- Created: 2022-03-14T21:09:23.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-23T09:54:45.000Z (4 months ago)
- Last Synced: 2025-03-09T02:50:30.561Z (4 months ago)
- Topics: dart, flutter, flutter-state-management, state, state-management, valuelistenablebuilder, valuenotifier
- Language: C++
- Homepage: https://pub.dev/packages/mu_state
- Size: 275 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# mu_state
A minimal state solution based on my **Pragmatic state handling in Flutter**
[Medium article ](https://medium.com/@erlendf/pragmatic-state-handling-in-flutter-d8c9bf5d7d2).## Features
A set of classes built on `ValueNotifier` and `ValueListenableBuilder` for
handling state in Flutter.- `MuState` - an alias for `ValueNotifier` where you can optionally use a `MuEvent` type
- `MuBuilder` - an alias for `ValueListenableBuilder`
- `MuEvent` - the base class for 3 state objects which can be used in a `MuState`:
- `MuData` - the data state of type `T`
- `MuError` - the error state
- `MuLoading` - the loading state
- `MuMultiBuilder` - listen to changes of a list of `Listenable` objects## How To
Contain state by inheriting from `MuState` or using it directly. This can have any type.
You can optionally use `MuEvent` types: `MuLoading`, `MuError` or `MuData` to contain state.
Listen to `MuState` changes using `MuBuilder`.
Listen to multiple `MuState` objects using `MuMultiBuilder`.
## Example
A simple counter state:
```Dart
import 'package:mu_state/mu_state.dart';class CounterState extends MuState {
CounterState(super.initValue);void increment() {
value = value + 1;
}
}final counterState = CounterState(0);
```In `main.dart`:
```Dart
Scaffold(
body: Center(
child: MuBuilder(
valueListenable: counterState,
builder: (context, event, child) {
return Text('$value');
},
),
),
),
```A state with loading and error handling:
```Dart
class LoadState extends MuState> {
LoadState(super.value);void load() async {
value = const MuLoading();
await Future.delayed(const Duration(milliseconds: 500));
value = const MuData('done');
}
}final loadState = LoadState(const MuData('initial'));
```Handle loading and error states:
```Dart
Scaffold(
body: Center(
child: MuBuilder(
valueListenable: loadState,
builder: (context, event, child) {
return switch (event) {
MuLoading() => const CircularProgressIndicator(),
MuError() => Text('Error: ${event.error}'),
MuData() => Text('Data: ${event.data}'),
};
},
),
),
),
```See more examples in [Example](./example/).