An extension to the bloc state management library which lets you create State Machine using a declarative API

Overview

BLoC State Machine

An extension to the bloc state management library which lets you create State Machine using a declarative API.

๐Ÿšง Work in progress. Do not use in production ๐Ÿšง

Overview

state_machine_bloc export a StateMachine class, a new kind of bloc designed to declare state machines using a nice builder API. It can be used in the same way as Cubit and Bloc and it aims to be compatible with the rest of the ecosystem.

state_machine_bloc supports:

  • Storing data in states
  • Defining state transitions based on event
  • Applying guard conditions to transitions
  • sideEffects
  • onEnter/onExit

State machines shines is in their expressiveness, predictability, and robustness:

  • This makes it easy to know all possible states of business logic
  • They eliminate bugs and weird situations because they won't let the UI transition to a state which we donโ€™t know about.
  • This eliminates the need for code that protects other codes from execution because They do not accept input that is not explicitly defined as acceptable for the current state.
((Stopped event, Running currentState) => Paused()), ); define ((b) => b ..onExit((Running currentState) => print("on exit paused state")) ..on ((Started event, Paused currentState) => Running()), ); } }">
class Timer extends StateMachine<Event, State> {
    Timer() : super(Running()) {
        define<Running>((b) => b
            ..onEnter((Running currentState) => print("on enter running state"))
            ..on<Stopped>((Stopped event, Running currentState) => Paused()),
        );

        define<Paused>((b) => b
            ..onExit((Running currentState) => print("on exit paused state"))
            ..on<Started>((Started event, Paused currentState) => Running()),
        );
    }
}

Additional ressources

Project Status

This project is very early and in active development. A basic Proof of Concept has been done so you can try it already. Any opinions, feedback, thought and contributions are welcome.

Roadmap

  • PoC
  • Make timer state machine example pass all original unit tests
  • Implements more bloc's example and maybe try new one
  • Define specs
  • Write some documentation
  • Alpha implementation

features to be explored

  • nested states machines
  • dedicated flutter builder/listener widgets
  • make state machine usable with other library than bloc

Getting Started

Install

add this to your pubspec.yaml

state_machine_bloc:
  git:
    url: git@github.com:Pierre2tm/state_machine_bloc.git
    path: packages/state_machine_bloc

then run flutter pub get.

import the package

import 'package:state_machine_bloc/state_machine_bloc.dart'

Usage

Declare your StateMachine:

timer.dart

((Stopped event, Running currentState) => Paused()), ); define ((b) => b ..onExit((Running currentState) => print("on exit paused state")) ..on ((Started event, Paused currentState) => Running()), ); } }">
import 'package:state_machine_bloc/state_machine_bloc.dart';

class Event {}
class Started extends Event {}
class Stopped extends Event {}

class State {}
class Running extends State {}
class Paused extends State {}


```dart
class Timer extends StateMachine<Event, State> {
    Timer() : super(Running()) {
        define<Running>((b) => b
            ..onEnter((Running currentState) => print("on enter running state"))
            ..on<Stopped>((Stopped event, Running currentState) => Paused()),
        );

        define<Paused>((b) => b
            ..onExit((Running currentState) => print("on exit paused state"))
            ..on<Started>((Started event, Paused currentState) => Running()),
        );
    }
}

Then use it as a regular Bloc:

timer_page.dart

BlocProvider(
    create: (_) => Timer(ticker: Ticker()),
    child: const TimerView(),
);

TODO: better examples TODO: detailed usage

You might also like...

Git+ is your ultimate GitLab mobile app that lets you interact with your projects like as if you were using desktop.

Git+ is your ultimate GitLab mobile app that lets you interact with your projects like as if you were using desktop.

Git+ for GitLab Git+ is your ultimate GitLab mobile app that lets you interact with your projects like as if you were using desktop. Git+ lets you see

Sep 21, 2022

Practice building basic animations in apps along with managing app state by BLoC State Management, Flutter Slider.

Practice building basic animations in apps along with managing app state by BLoC State Management including: Cubit & Animation Widget, Flutter Slider.

Jun 8, 2022

Flutter App Build for the machine Learning model which shows sentiments of instagram user by analysing their captions

Flutter App Build for the machine Learning model which shows sentiments of instagram user by analysing their captions

InstaKnow Front-end By @ketanchoyal Back-end By @namas191297 Front-end Application Flutter application that allows user to analyze sentiments of other

Apr 24, 2022

A Simplified, Declarative Implementation Of CustomPaint For Flutter

โœ๏ธ Etch A Simplified, Declarative Implementation Of CustomPaint For Flutter Features Create your CustomPaint widget declaratively. EtchCanvas(

Sep 16, 2022

Nexus is a state management library that makes it easy to create and consume your application's reactive data to the user interface.

Nexus ๐Ÿš€ Nexus is a state management library that makes it easy to create and consume your application's reactive data to the user interface. With nex

Sep 7, 2022

Easy Form State Management using BLoC pattern

๐Ÿ”ฅ Dart and Flutter Package ๐Ÿ”ฅ Easy Form State Management using BLoC pattern ๐Ÿ”ฅ Wizard/stepper forms, asynchronous validation, dynamic and conditional fields, submission progress, serialization and more! ๐Ÿ”ฅ

Sep 18, 2022

A simple to-do list built using flutter based on BLoC state management to manage your daily tasks .

A simple to-do list built using flutter based on BLoC state management to manage your daily tasks .

๐Ÿ“ Table of Contents About ScreenShots from the app Demo vedio Contributors About A to-do list flutter app to manage your daily tasks. it is built bas

Sep 12, 2022

Flutter Control is complex library to maintain App and State management. Library merges multiple functionality under one hood. This approach helps to tidily bound separated logic into complex solution.

Flutter Control is complex library to maintain App and State management. Library merges multiple functionality under one hood. This approach helps to tidily bound separated logic into complex solution.

Flutter Control is complex library to maintain App and State management. Library merges multiple functionality under one hood. This approach helps to

Feb 23, 2022

App for visually impaired using Machine Learning, Artificial Intelligence, and Flutter.

App for visually impaired using Machine Learning, Artificial Intelligence, and Flutter.

Your Eyes ๐Ÿ“Œ Introduction Our Application aims to bridge the gap between them and the visual world by Deep Learning and lucid User-Interface for easy

Jan 2, 2022
Comments
  • [Discussion] about state machine with love ๐Ÿ˜„

    [Discussion] about state machine with love ๐Ÿ˜„

    Hi there,

    Thank for your great work to develop this library! I'm pretty happy to see this state management solution, and feel it a bit similar to one I'm currently using: https://pub.dev/packages/love I'm interesting in state management area, so I'd like to discuss about different soluttions. What do you thing about state machine with love ๐Ÿ˜„?

    discussion 
    opened by beeth0ven 16
  • Redefining state definition API

    Redefining state definition API

    Redefining StateMachine's state definition builder API.

    StateMachine definition API is being reworked and will change in the next release.

    state's transition:

    State transition remains unchanged. They are registered by calling stateDefinitionBuilder's on function. They are in the form:

    FutureOr<State?> Function(Event, DefinedState)
    

    Transitions are evaluated sequentially. They are always awaited. If transition returns a non-null state, the state machine stops evaluating transitions and changes its inner state. If transition return null, transition is considered refused and the next transition is evaluated.

    state's side effects:

    Side effects are callback functions that you can register for each state. They are a good place for calling repository functions. Side effects are in the form:

    void function(State currentState)
    

    They can be async but they will not be awaited. If you need to update/change state machine state based inside side effect, you should add an event to the StateMachine with add function.

    You have access to 3 side effects slot:

    onEnter: called when entering state if currentState.runtimeType != nextState.runtimeType

    onChange: called when state data updated, if currentState.runtimeType == nextState.runtimeType

    onExit: called when exiting state, if currentState.runtimeType == nextState.runtimeType

    Example

    class Example extends StateMachine<Event, State> {
      Example() : super(Counter(0)) {
        define<Counter>((b) => b
          ..onEnter((Counter state) { /*
          called on entering. In this case when the StateMachine will be constructed
          */ }),
          ..on<ButtonPressed>((ButtonPressed event, Initial state) =>
              state.value < 10 ? Counter(state.value + 1) : Done()
          ),
          ..onChanged((Counter state) => print(state.value)),
          ..onExit((Initial state) { /* state.value equal 10 */ }),
        );
        define<Done>((b) => b
          ..onEnter((Done state) { /* called on entering */ }),
          ..onChange((Done state) { /* Never called in this case ! */ }),
        );
    }
    

    What do you think about the new api? Do you have any suggestions? Give your feedback in the comments below :)

    enhancement 
    opened by Pierre2tm 2
Releases(v0.0.1)
Owner
null
This is machine learning project where developed the model in teachable machine.

doc_help A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if this

Abu sayed chowdhury 1 Apr 26, 2022
A powerful state machine for MobX management, that can be used in almost any application state.

A powerful state machine for MobX management, which can be used in almost any application state. It has 3 states - loading, success, error - and is pe

Daniel Magri 6 Sep 6, 2022
This is a flutter app which uses the Bitrise Api(https://api-docs.bitrise.io/) to show the bitrise projects and builds and lets you download your artifacts.

Bitrise Artifact Downloader Introduction ??โ€โ™‚๏ธ This is a flutter app which uses the Bitrise Api(https://api-docs.bitrise.io/) to show the bitrise proj

Jens Klingenberg 9 Apr 30, 2021
Flutter bloc example - An app for State management using BLoC pattern in Flutter

Flutter BLoC My first app for State management using BLoC pattern in Flutter Col

Harshil Patel 1 Jun 16, 2022
Petrus Nguyแป…n Thรกi Hแปc 169 Sep 19, 2022
flutter_bloc state management extension that integrates sealed_unions.

flutter_bloc meets sealed_unions Sponsors Our top sponsors are shown below! [Become a Sponsor] Try the Flutter Chat Tutorial ?? Quick Start Extend Uni

Felix Angelov 70 Aug 16, 2022
A routing package that lets you navigate through guarded page stacks and URLs using the Router and Navigator's Pages API, aka "Navigator 2.0".

A Flutter package to help you handle your application routing and synchronize it with browser URL. Beamer uses the power of Router and implements all

Sandro Lovniฤki 453 Sep 23, 2022
A powerful official extension library of Tab/TabBar/TabView, which support to scroll ancestor or child Tabs when current is overscroll, and set scroll direction and cache extent.

extended_tabs Language: English | ไธญๆ–‡็ฎ€ไฝ“ A powerful official extension library of Tab/TabBar/TabView, which support to scroll ancestor or child Tabs whe

FlutterCandies 172 Sep 24, 2022