rebloc library

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 widgets.

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 differences:

  • 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 required converter function, 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 dispatcher method, so new Actions can be dispatched in response to user events like button presses.

Classes

Accumulator<S>
An accumulator for reducer functions.
Action
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.
Bloc<S>
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.
DebouncerBloc<T>
Debounces repeated dispatches of an Action or list of Actions.
DispatchSubscriber<S>
Retrieves a DispatcherFunction from an ancestor StoreProvider, and builds builds widgets that can use it.
FirstBuildDispatcher<S>
Dispatches action to an inherited Store the first time it builds.
SimpleBloc<S>
A convenience Bloc class that handles the stream mapping bits for you. Subclasses can simply override middleware, reducer, and afterware to add their implementations.
Store<S>
A store for app state that manages the dispatch of incoming actions and controls the stream of state objects emitted in response.
StoreProvider<S>
A StatelessWidget that provides Store access to its descendants via a static of method.
ViewModelSubscriber<S, V>
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.
WareContext<S>
The context in which a middleware or afterware function executes.

Typedefs

DispatchFunction = void Function(Action action)
A function that can dispatch an Action to a Store.
DispatchWidgetBuilder = Widget Function(BuildContext context, DispatchFunction dispatcher)
Widget builder function that includes a dispatcher capable of dispatching an Action to an inherited Store.
ViewModelConverter<S, V> = V Function(S state)
Creates a new view model instance from the given state object. This method should be used to narrow or filter the data present in state to the minimum required by the ViewModelWidgetBuilder the converter will be used with.
ViewModelWidgetBuilder<S, V> = Widget Function(BuildContext context, DispatchFunction dispatcher, V viewModel)
Accepts a BuildContext and ViewModel and 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.