- well, its simple to use
# add the dependencies
simple_finite_state_machine:
git: https://github.com/raj457036/simple_finite_state_machine.git
credit: https://github.com/Tinder/StateMachine
Below is a simple example based on above diagram
// states
class Solid extends MachineState<Solid> {
@override
get transitions => [Melt];
}
class Liquid extends MachineState<Liquid> {
@override
get transitions => [Vaporize, Freeze];
}
class Gas extends MachineState<Gas> {
@override
get transitions => [Condense];
}
// transitions
class Melt extends StateTransition<Solid> {
@override
MachineState? target(current) {
return Liquid();
}
}
class Vaporize extends StateTransition<Liquid> {
@override
MachineState? target(current) {
return Gas();
}
}
class Condense extends StateTransition<Gas> {
@override
MachineState? target(current) {
return Liquid();
}
}
class Freeze extends StateTransition<Liquid> {
@override
MachineState? target(current) {
return Solid();
}
}
// main program
void main(List<String> args) {
final machine = Machine(Solid(), logging: true);
machine.execute(Melt());
machine.execute(Vaporize());
machine.execute(Condense());
machine.execute(Freeze());
// This one will fail
machine.execute(Condense());
}
The example above is from Tinder State Machine Example