Skip to content
pubscout

dart_fsm

1.3.1, released 4 months ago by an unverified publisher

A package for implementing finite state machines in Dart with a DSL and side effect handling.

WASM-ready
$ flutter pub add dart_fsm
Source repository

What it does

Allows defining states, actions, and transitions using a graph builder DSL. It automates state transitions, supports side effects before or after transitions, and provides a Given-When-Then testing tool.

AI-written summary of version 1.3.1, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You want to manage application state predictably using a finite state machine.
  • Your state transitions involve complex logic or require automated side effects like API calls.
  • You need comprehensive testing tools to verify state transitions and side effects.

Skip it when

  • Your application has extremely simple state management needs where a state machine adds unnecessary overhead.
  • You prefer UI-integrated state management solutions like Riverpod or Bloc for Flutter widget rebuilding.

Strengths

  • Clear DSL for defining states and transition graphs.
  • Built-in handling for side effects and stream subscriptions.
  • Exhaustive test runner DSL for Given-When-Then state transition testing.

Weaknesses

  • Requires manual setup of states, actions, and transition graphs compared to simpler state containers.
  • Limited to finite state machine patterns.

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
3
final stateMachineGraph = GraphBuilder<SampleState, SampleAction>()
  ..state<SampleStateA>(
    (b) => b
      ..on<SampleActionA>(
        (state, action) => b.transitionTo(const SampleStateB()),
      ),
  );

final stateMachine = createStateMachine(
  initialState: const SampleStateA(),
  graphBuilder: stateMachineGraph,
);

stateMachine.dispatch(const SampleActionA());

Alternatives

Packages that do the same job and could replace dart_fsm.

  • bloc

    If you want a more common, event-driven state management library designed for Flutter with extensive community support.

  • riverpod

    If you prefer a robust dependency injection and reactive state management approach without explicitly designing state transition graphs.

Works with

Packages commonly used together with dart_fsm: ones it builds on, ones built on it, and its own family.