This repository contains a minimal implementation of Cubits from package:flutter_bloc in Flutter.
Whilst I don't make use of the Bloc state management system, I am making use of its little brother: Cubit. In the rest of this README you'll find my notes synthesising what I was able to learn from the generous sources listed below.
Documentation and Tutorials:
package:flutter_bloc- Dart Docs- Core concepts (package:flutter_bloc) - bloc
- What is the BLoC Pattern? - flutterclutter.dev
- Cubits in Flutter: A Practical Guide - Amos Gross
The difference: BLoC
Blocs and Cubits are not widgets or data representation. Instead they stand between those two things, exposing functionality to the widgets so that actions can be invoked from the UI, and manipulating data in a controlled manner. They help prevent tight coupling between an application's data and presentation layers.
Architectural overview:
- State: An object (encapsulated data model/representation) relevant to some aspect of an application. It is the thing actually being passed around--the state being managed.
- State Manager: An object existing between UI and state which handles the manipulation of data based on user interaction with the UI layer, and the subsequent delivery of updated state back to the UI from the data layer.
- Provider: A widget which should be inserted high up into the tree such that its subtree contains the widgets which uses the state(s) that it is providing and managing.
- Consumer: A widget which should be inserted into a provider's subtree such that it can access it via
context.
State Manager:
Bloc: An object which exposes event triggers the UI layer and contains event-handling procedures to manipulate the data layer and output resultingstates accordingly.Cubit: An object passed down through the UI layer exposing methods which can directly manipulatestateand subsequently return it to be provided again. It is essentially aBlocwhich exposes methods which can be directly called, as opposed to a suite of events, thereby "removing a step".
Provider Widgets:
BlocProvider: Initialises aBlocorCubitobject and contains a subtree which has consumers.MultiBlocProvider: Contains a listBlocProviderto avoid nestingBlocProviderswhere multiple are necessary, thereby creating multiple instances ofBlocs orCubits for multifaceted state management.
Consumer Widgets:
BlocBuilder: Builds a new widget in response to a change instate.BlocListener: Listens for a change instateand calls thelistenercallback function exactly once for each change instate.MultiBlocListener: Contains a list ofBlocListeners to avoid nestingBlocListeners where multiple are necessary, thereby containing multiplelisteners to respond to changes instatein a multifaceted manner.BlocComsumer: Literally combines the functionality ofBlocBuilderandBlocListenertogether into a single widget to allow for both alistenerandbuildercallback.BlocSelector: Calls itsselectorcallback which returns astateto be used by thebuildercallback when building the widget, essentially allowing for preprocessing of astateprior to building.
Utility Classes/Typedefs:
Change: A class for representing a change in state comprised acurrentStateandnextStatefield holdingstates of the same type but possibly having different values. They are created whenever a state change occurs, being accessible using a BlocObserver for monitoring purposes.Emitter: A class used by anEventHandlerwith acall()method which emits a state.EventHandler: A typedef representing a function tying an event (which can be any type of data) to anEmitterwhich emits a state in response. They are responsible for reacting to incoming events and emitting zero or more states in response when using aBloc.Transition: A class for representing a change from one state to another along with the event which incited that change. They are created whenever a state change occurs as a result of an event, being accessible using aBlocObserverfor monitoring purposes.