a simple yet powerful state management technique for Flutter

  packages, Packages, State Management

states_rebuilder

states_rebuilder is a flutter state management solution that allows for clear and sharp separation of concern between the user interface (UI) logic and the business logic. The separation is clear and sharp to the point that the business logic is written with pure, vanilla, plain old dart classes without extending any external library-specific classes and without annotation or code generation.

Business logic

The business logic classes are independent of any external library. They are independent even from states_rebuilder itself.

The only constraint imposed by states_rebuilder is to ensure that methods supposed to be called from the user interface must return anything; their only role is to mutate the state (aka fields) of the class.

Another advantage of states_rebuilder is that it has practically no boilerplate. It has no boilerplate to the point that you do not have to monitor the asynchronous state yourself. You do not need to add fields to hold for example onLoadingonLoadedonError states. states_rebuilder automatically manages these asynchronous statuses and exposes the isIdle, isWaitinghasError and hasData getters and onIdleonWaitingonError and onData hooks for use in the user interface logic.

With states_rebuilder, you write business logic without bearing in mind how the user interface would interact with it.

UI logic

With states_rebuilder, you write your user interface using StatelessWidget, and when you need a business logic class, you just inject a singleton of the class using the Injector widget and get it from any child widget using the Injector.get method.

The instance obtained using Injector.get is not reactive, it is the registered singleton of the pure dart class. To make a vanilla dart class reactive, just get it using the Injector.getAsReactive method.

The model obtained is of ReactiveModel type and it is observable in the context of observer pattern. Observer widgets can subscribe to it and the observable reactive model can notify them to rebuild.

To subscribe to a ReactiveModelstates_rebuilder offers StateBuilderStateWithMixinBuilderWhenRebuilderWhenRebuilderOr and OnSetStateListener widgets.

To notify observers you call setState or setValue methods of an observable model.

With states_rebuilder, you can create as many ReactiveModels as you want from the same object, so that you can surgically control the part of the widget tree to rebuild.

`states_rebuilder` is Flutter state management combined with a dependency injection solution and an integrated router to get the best state management experience and speed up your development.

`states_rebuilder` is a feature-rich state management sulution

  • Performance
    • Predictable and controllable state mutation
    • Immutable / Mutable states support
    • Strictly rebuild control
    • Auto clean state when not used
  • Code Clean
    • Zero Boilerplate
    • No annotation & code-generation
    • Separation of UI & business logic
    • Achieve business logic in pure Dart.
  • User Friendly
    • Built-in dependency injection system
    • SetState in StatelessWidget.
    • Hot-pluggable Stream / Futures
    • Easily Undo / Redo
    • Elegant error handling and refreshing
    • Navigate, show dialogs without BuildContext
    • Named route with dynamic segment
    • Nested routes mapping
    • Easily persist the state and retrieve it back
    • Override the state for a particular widget tree branch (widget-wise state)
  • development-time-saving
    • Easily CREATE, READ, UPDATE, and DELETE (CRUD) from rest-API or database.
    • Easy user authentication and authorization.
    • Easily app themes management.
    • Simple internalization and localization.
  • Maintainable
    • Easy to test, mock the dependencies
    • state tracker middleware
    • Built-in debugging print function
    • Capable for complex apps
cheat sheet

Examples:

Basics:

You are new to states_rebuilder this is right place to start from. The order is important:

  • The simplest counter app : Default flutter counter app refactored using states_rebuilder. You will understand the concept of ReactiveModel and how to make a pure dart class reactive. You will see the use of ReactiveModel.createsetValueisIdleisWaitinghasDatahasErroronIdleonWaitingonErroronDatawhenConnectionStateStateBuilder and WhenRebuilder.
  • Login form validation Simple form login validation. The basic ReactiveModel concepts are put into practice to make form validation one of the easiest tasks in the world. The concept of exposed model is explained here.
  • counter app with flavors Injector as dependency injection is used, and a counter app with two flavors is built. You will see the use of InjectorInjector.getInjector.getAsReactiveInject.interfaceInject.env, and setState.
  • countDown timer This is a timer that ticks from 60 and down to 0. It can be paused, resumed or restarted. You see how to inject enumeration and how to make two reactive models interact. You will see the use of OnSetStateListener.
  • double async counters Two counter that share the same model. You will see the use of tags to filter notifications and the concept of new reactive models to limit rebuild process. You will see tagfilteredTags and asNew
  • multi async counters The is a solution of an imaginary and very tricky state management requirement. You will see how the concept of ReactiveModel can solve very difficult state management requirements. You will see How can Reactive singleton interact with new reactive models. You will use joinSingletonJoinSingleton.withCombinedReactiveInstancesjoinSingletonToNewData

Architecture

  • User posts and comments The app communicates with the JSONPlaceholder API, gets a User profile from the login using the ID entered. Fetches and shows the Posts on the home view and shows post details with an additional fetch to show the comments.
  • firebase login The app uses firebase for sign in. The user can sign in anonymously, with google account, with apple account or with email and password.
  • firebase realtime database The app add, update, delete a list of counters from firebase realtime database. The app is built with two flavors one for production using firebase and the other for test using fake data base.

Note that all of the above examples are tested. With states_rebuilder, testing your business logic is the simplest part of your coding time as it is made up of simple dart classes. On the other hand, testing widgets is no less easy, because with states_rebuilder you can isolate the widget under test and mock its dependencies.

List of article about states_rebuilder

Download Flutter state management Source code on GitHub

https://github.com/GIfatahTH/states_rebuilder