state_graph_bloc 5.0.0 copy "state_graph_bloc: ^5.0.0" to clipboard
state_graph_bloc: ^5.0.0 copied to clipboard

A bloc package with concise state graph builder. Build transitions, side effects and global events in a centralised, type-safe manner.

state_graph_bloc #

State Graph Builder for Dart. Allows to define states, their transitions and side effects in a result of incoming events.

Inspired by Tinder's state machine Kotlin DSL and BLoC package.

Usage #

Build a bloc and subscribe with StreamBuilder or listen().

class DummyBloc extends StateGraphBloc<DummyEvent, DummyState> {
  DummyBloc() : super(StateInitialising());
  
  @override
  StateGraph<DummyEvent, DummyState> buildGraph() =>
      StateGraph<DummyEvent, DummyState>(
        {
          StateInitialising: {
            LoadEvent: transitionWithSideEffect(
              (state, event) => StateLoading(),
              (state, event) {
                // There could be some async loading. Use [launchFuture] or [launchStream]
                // to fire async processing and properly handle bloc closing.
                add(LoadingCompletedEvent());
              },
            ),
          },
          StateLoading: {
            LoadingCompletedEvent: transition((state, _) => StateReady(0)),
          },
          StateReady: {
            IncrementEvent: transition(
              (state, event) => StateReady((state as StateReady).counter + 1),
            ),
            // Test global event override.
            RestartEvent: transition((state, _) => StateReady(0)),
          },
        },
        globalEvents: {
          RestartEvent: transition((state, event) {
            return StateInitialising();
          })
        },
      );

  // Bind some variables based on [state].
  Stream<bool> isLoading() => bindState((state) {
        if (state is StateLoading) {
          return true;
        } else {
          return false;
        }
      });
}

Samples #

test directory contains all State Graph Bloc use cases.

8
likes
140
pub points
0%
popularity

Publisher

verified publisherjelenski.net

A bloc package with concise state graph builder. Build transitions, side effects and global events in a centralised, type-safe manner.

Homepage
Repository (GitLab)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

rxdart

More

Packages that depend on state_graph_bloc