statemachine 1.3.0

  • Installing
  • Versions
  • 86

State Machine

Pub Package Build Status Coverage Status GitHub Issues GitHub Forks GitHub Stars GitHub License

A simple, but generic state machine framework for Dart.

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.

Up-to-date class documentation is created with every release.

This code was inspired by work of Vassily Boykov on his Smalltalk Announcement framework.



Follow the Installing instructions on

Import the package into your Dart code using:

import 'package:statemachine/statemachine.dart';

Creating a machine

To create a new state machine instantiate Machine:

var machine = new Machine();

Defining states

To create states call Machine.newState and store them in variables. The first state created it the start state of the machine. Optionally you can provide a name as argument to ease debugging.

var startState = machine.newState();
var activeState = machine.newState('active');

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.

activeState.onEntry(() => print('activated'));
activeState.onExit(() => print('deactivate'));

Starting and stopping a machine

To start a state machine and set its state to its starting state call 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.


Event triggered transition

You can define transitions between states that are triggered by events of any kind 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 active state:

startState.onStream(element.onClick, (value) => activeState.enter());

Future completion transition

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.

startState.onFuture(computation, (value) => activeState.enter());

Time based transition

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 the inactive state:

activeState.onTimeout(const Duration({seconds: 1}), () => startState.enter());

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.




The MIT License, see LICENSE.



  • Reformat all code.
  • Reorganize into micro libraries.


  • Replace deprecated async code.


  • Fix CSS problem in example.


  • Update documentation.


  • Fix linter warnings.
  • Update documentation.
  • Address missing coverage.


  • Fix a broken test.


  • Update to use Travis.


  • Support for nested machines.
  • Support for enter/exist state events.


  • Improve test coverage.


  • Initial version.

1. Depend on it

Add this to your package's pubspec.yaml file:

  statemachine: "^1.3.0"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:statemachine/statemachine.dart';
Version Uploaded Documentation Archive
1.3.0 Aug 4, 2017 Go to the documentation of statemachine 1.3.0 Download statemachine 1.3.0 archive
1.2.3 Dec 19, 2016 Go to the documentation of statemachine 1.2.3 Download statemachine 1.2.3 archive
1.2.2 Oct 17, 2016 Go to the documentation of statemachine 1.2.2 Download statemachine 1.2.2 archive
1.2.1 Apr 5, 2016 Go to the documentation of statemachine 1.2.1 Download statemachine 1.2.1 archive
1.2.0 Oct 6, 2015 Go to the documentation of statemachine 1.2.0 Download statemachine 1.2.0 archive
1.1.1 May 15, 2015 Go to the documentation of statemachine 1.1.1 Download statemachine 1.1.1 archive
1.1.0 May 15, 2015 Go to the documentation of statemachine 1.1.0 Download statemachine 1.1.0 archive
1.0.6 Feb 28, 2015 Go to the documentation of statemachine 1.0.6 Download statemachine 1.0.6 archive
1.0.5 Dec 30, 2014 Go to the documentation of statemachine 1.0.5 Download statemachine 1.0.5 archive
1.0.4 Aug 16, 2014 Go to the documentation of statemachine 1.0.4 Download statemachine 1.0.4 archive

All 21 versions...


This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Feb 3, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1


Describes how popular the package is relative to other packages. [more]
72 / 100
Code health derived from static analysis. [more]
100 / 100
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:statemachine/statemachine.dart.


  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use statemachine.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.0 <2.0.0
Dev dependencies
browser ^0.10.0
test ^0.12.0