Package selection guidance compiled from public package information, community data, and project usage.
A lightweight and flexible state machine framework for Dart, designed to simplify state management in Flutter and web applications. It provides a clear structure for defining states, transitions, and actions, enabling predictable and maintainable application logic. The package supports both synchronous and asynchronous transitions, making it suitable for complex workflows like form handling, authentication flows, and UI navigation. Built with simplicity and performance in mind, it integrates seamlessly into existing projects without heavy dependencies.
Use cases
Managing complex UI workflows
Handling multi-step forms
Implementing authentication flows
Controlling app navigation
Modeling game or process states
Key features
Simple API for defining states and transitions
Support for synchronous and asynchronous actions
Built-in event-driven architecture
Extensible through custom guards and listeners
Lightweight and dependency-free
Suitable for
Developers seeking structured state management
Projects requiring predictable state transitions
Apps with complex user interactions
Teams wanting minimal boilerplate
Considerations
Not ideal for simple state changes
Requires understanding of state machine concepts
Limited built-in debugging tools
Best suited for apps with well-defined state boundaries
Original README
Original README snapshot from the linked source repository. Visit the repository for the latest content.
State Machine
A simple, yet powerful state machine framework for Dart supporting Flutter and web apps.
This library is open source, stable and well tested. Development happens on GitHub. Feel free to report issues or create a pull-request there. General questions are best asked on StackOverflow.
Follow the installation instructions on dart packages.
Import the package into your Dart code using:
import'package:statemachine/statemachine.dart';
Creating a machine
To create a new state machine instantiate Machine:
final machine = Machine<String>();
The type of the state machine is used to uniquely identify states of the machine. In this simple example we use a String; but typically you would use an enum, a Symbol, or an arbitrary other identifying object.
Defining states
To create states call Machine.newState and store them in variables. Each state needs an identifying key of the declared type, as described above we use a String that helps debugging.
final startState = machine.newState('start');
final activeState = machine.newState('active');
By default, the first state created is also the start state of the machine. It is possible to explicitly create start and stop states of the machine using Machine.newStartState and Machine.newStopState.
Callbacks on states
States support callbacks whenever a state is entered or left.
To start a state machine and set its state to its starting state call Machine.start:
machine.start();
Similarly, you can stop a machine by calling Machine.stop.
Transitioning between states
There are various ways in which your machine can switch states.
Manually triggered transition
From anywhere within your code you can enter a specific state by calling State.enter.
someState.enter();
Depending on context, it might be easier to set the current state using the accessor on the state machine itself:
machine.current = someState;
Alternatively, you can also use the identifying object to set the active state:
machine.current = 'active';
Event triggered transition
You can define transitions between states that are triggered by events using State.onStream. The example below registers for click events when the inactive state is entered. In case of a click event the callback is executed, and the state machine transitions into the new state:
Also, transitions can be triggered by the completion of a future using State.onFuture. Since futures cannot be suspended or cancelled, the future continues to run even if the owning state is deactivated. Should the state be activated, and the future value is already present, then the value is immediately supplied into the callback. Further activations have no effect.
Also, you can automatically trigger callbacks after a timeout using State.onTimeout. The following snippet calls the callback 1 second after the active state is entered and falls back to another state:
Callbacks often contain code to check for additional constraints and update other objects or UI element before entering a different state. See the tooltip example directory for a more complete illustration of the functionality provided by this library.
Nested machines
Machines can be nested. Simply add another machine that gets started when the state is entered, and stopped when the state is left.