mu_state 0.3.0+3 copy "mu_state: ^0.3.0+3" to clipboard
mu_state: ^0.3.0+3 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 for handling state in Flutter.

  • MuState - a ValueNotifier of type MuEvent<T>
  • MuBuilder - a ValueListenableBuilder of type MuEvent<T>
  • MuEvent<T> - the base class for our 3 state object
    • MuEventData<T> - the data state of type T
    • MuEventError - the error state
    • MuEventLoading - the loading state
  • MuMultiBuilder - listen to changes in a list of MuState objects

How To #

Declare state as a MuState<T> variable and pass it an initial MuEvent type, e.g. a MuEventLoading or a MuEventData<T>.

For more complex state objects, extend MuState and implement the necessary methods.

Listen to MuState changes using MuBuilder.

Listen to multiple MuState objects using MuMultiBuilder.

Example #

In test_state.dart:

import 'package:mu_state/mu_state.dart';

class CounterState extends MuState<int> {
  CounterState(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.

2
likes
140
pub points
43%
popularity

Publisher

verified publisherapptakk.com

A set of helpers for pragmatic state handling as mentioned in my Medium article.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter

More

Packages that depend on mu_state