Skip to content
pubscout

saga_state_machine

1.1.2, released 8 months ago by an unverified publisher

A MassTransit-style state machine framework for Dart with a fluent builder API.

WASM-ready
$ flutter pub add saga_state_machine
Source repository

What it does

A declarative event-driven saga state machine framework that coordinates multiple events over time. It persists state across events, correlates events using a unique identifier, handles timeouts automatically, and supports compensation and rollback.

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

Use it when

  • You need to coordinate long-running business workflows or saga processes across multiple events.
  • You want a declarative state machine API inspired by MassTransit in Dart.
  • You require event correlation by ID and automatic timeout handling for specific states.

Skip it when

  • You only need simple UI state management like ValueNotifier or simple BLoCs.
  • You want an extensive built-in persistence backend beyond in-memory storage.

Strengths

  • Declarative and fluent builder API for defining state machines.
  • Built-in support for event correlation, timeouts, and state compensation.
  • Pluggable storage repository interface for custom persistence.

Weaknesses

  • Default repository is in-memory only; custom persistence requires manual implementation.
  • Niche state machine architectural pattern which may be overkill for simple apps.

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
1
class OrderStateMachine extends SagaStateMachine<OrderSaga, OrderStatus> {
  OrderStateMachine() {
    correlate<OrderCreated>((e) => e.orderId);
    correlate<PaymentReceived>((e) => e.orderId);

    initially(
      when<OrderCreated>()
        .set((saga, e) => saga
          ..customerId = e.customerId
          ..amount = e.amount)
        .transitionTo(OrderStatus.pending),
    );

    during(OrderStatus.pending,
      when<PaymentReceived>().transitionTo(OrderStatus.paid),
      timeout(Duration(hours: 24), transitionTo: OrderStatus.cancelled),
    );
  }

  @override
  OrderSaga createSaga(String id) => OrderSaga()..id = id;

  @override
  OrderStatus getState(OrderSaga saga) => saga.status;

  @override
  void setState(OrderSaga saga, OrderStatus state) => saga.status = state;
}

Alternatives

Packages that do the same job and could replace saga_state_machine.

  • yx_state

    A state management library for Dart and Flutter applications.

  • cached_query

    Library for caching, background fetching, and managing server state in Dart and Flutter.

  • statemachine

    A typed state machine library for Dart and Flutter allowing explicit state definitions and transitions.

  • refena

    A state management and dependency injection library inspired by Riverpod and async_redux.