Rebloc is a modified implementation of the Redux pattern using techniques more idiomatic to Flutter and Dart. Plus there's blocs.
Here, Bloc is used to mean a business logic class that accepts input and creates output solely through streams. Each Bloc is given two chances to act in response to incoming actions: first as middleware, and again as a reducer.
Rather than using functional programming techniques to combine reducers and middleware, Rebloc uses a stream-based approach. A Rebloc Store creates a dispatch stream for receiving new Actions, and invites Blocs to subscribe and manipulate the stream to apply their middleware and reducer functionality. Where in Redux, one middleware function is responsible for calling the next, with Rebloc a middleware function receives an action from its input stream and (after logging, kicking off async APIs, dispatching new actions, etc.) is responsible for emitting it on its output stream so the next Bloc will have a chance to act. If the middleware function needs to cancel the action, of course, it can just emit nothing.
After middleware processing is complete, the Stream of Actions is mapped
to one of Accumulators, and each Bloc is given a chance to apply reducer
functionality in a similar manner. When finished, the resulting app state is
added to the Store's
states stream, and changes can be picked up by
ViewModelSubscriber is intended to be the sole mechanism by which widgets
are built from the data emitted by the Store and wired up to dispatch
Actions to it. ViewModelSubscriber is similar to
StreamBuilder in that
it listens to a stream and builds widgets in response, but with a few key
- It looks for a StoreProvider ancestor in order to get a reference to a Store of matching type.
- It assumes the stream will always have a value on subscription (an RxDart BehaviorSubject is used by Store to ensure this). As a result, it has no mechanism for connection states or snapshots that don't contain data.
- It converts the app state objects it receives into view models using a
converterfunction, and ignores any changes to the app state that don't cause a change in its view model, limiting rebuilds.
- It provides to its builder method not only the most recent view model, but
also a reference to the Store's
dispatchermethod, so new Actions can be dispatched in response to user events like button presses.
- An accumulator for reducer functions. [...]
- A Redux-style action. Apps change their overall state by dispatching actions to the Store, where they are acted on by middleware, reducers, and afterware in that order.
- A Business logic component that can apply middleware, reducer, and afterware functionality to a Store by transforming the streams passed into its applyMiddleware, applyReducer, and applyAfterware methods.
- Debounces repeated dispatches of an Action or list of Actions. [...]
DispatcherFunctionfrom an ancestor StoreProvider, and builds builds widgets that can use it. [...]
- Dispatches action to an inherited Store the first time it builds. [...]
- A convenience Bloc class that handles the stream mapping bits for you. Subclasses can simply override middleware, reducer, and afterware to add their implementations.
- A store for app state that manages the dispatch of incoming actions and controls the stream of state objects emitted in response. [...]
- A StatelessWidget that provides Store access to its descendants via a static of method.
- Transforms a stream of state objects found via StoreProvider into a stream of view models, and builds a Widget each time a distinctly new view model is emitted by that stream. [...]
- The context in which a middleware or afterware function executes. [...]
Action action) → void
- A function that can dispatch an Action to a Store.
BuildContext context, DispatchFunction dispatcher) → Widget
Widget builder function that includes a
dispatchercapable of dispatching an Action to an inherited Store.
S, V>( S state) → V
Creates a new view model instance from the given state object. This method
should be used to narrow or filter the data present in
stateto the minimum required by the ViewModelWidgetBuilder the converter will be used with.
S, V>( BuildContext context, DispatchFunction dispatcher, V viewModel) → Widget
Accepts a BuildContext and
ViewModeland builds a Widget in response. A DispatchFunction is provided so widgets in the returned subtree can dispatch new actions to the Store in response to UI events.