An example Todo app created with the frideos library.
- This example aims to implement the BLoC pattern in a slightly different way, trying to reduce the amount of code and complexity of the original implementation, making it a little bit easier to adopt by beginners.
- A singleton instance of the
AppStateclass implementing the
AppStateModelinterface, holds the state of the app, here is where the only two BLoCs
- The state of the app is provided to the widgets tree by the
AppStateProvider, a StatefulWidget that uses an InheritedWidget to make possible for the widgets of the subtree to access the data.
- Instead of one BLoC per screen, in this sample a single BLoC,
TodosBloc, contains the business logic of the screens that share similar needs (
- To send data from the
StatsBloc, in order to calculate the number of active and completed todos, it is used the
sendmethod of the
ListSenderclass. First, in the
TodosBlocis created an instance of the
ListSenderclass, then by its method
setReceiver, in the
AppStateclass is set a reference to the stream on the
StatsBlocthat will receive the todos list whenever the method
sendis called. In this case, there is no need for the two BLoCs to share the same source of data, or having to pass the
TodosBlocas a parameter to the
StatsBlocto get the todos list.
UI and Streams
- As per the classic BLoC implementation, the widgets, most of which in this sample are Stateless, are automatically rebuilt whenever the streams emit a new event.
- Every time a new value is set, the
ValueBuilderwidget, which takes as a parameter an object implementing the StreamedObject interface of the library, rebuilds providing the updated data. This is just a widget that extends the
StreamBuilderand adds some callbacks to handle the stream state and return a
Containerif no widget is passed to the
NoDataChildparameter, in order to avoid to check for the
snapshot.hasDataproperty to not return a null widget, ultimately, resulting in a less and cleaner code.
There are no particular tricks for testing the app with this library. The sample was tested with unit tests that check for every feature of the apps, and by the integration test with flutter drive.