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 theAppStateModelinterface, holds the state of the app, here is where the only two BLoCsTodosBlocandStatsBlocare instantiated. - 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 (HomeScreen,DetailScreenandAddEditScreen). - To send data from the
TodosBlocto theStatsBloc, in order to calculate the number of active and completed todos, it is used thesendmethod of theListSenderclass. First, in theTodosBlocis created an instance of theListSenderclass, then by its methodsetReceiver, in theAppStateclass is set a reference to the stream on theStatsBlocthat will receive the todos list whenever the methodsendis called. In this case, there is no need for the two BLoCs to share the same source of data, or having to pass theTodosBlocas a parameter to theStatsBlocto get the todos list.
- 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 theStreamBuilderand adds some callbacks to handle the stream state and return aContainerif no widget is passed to theNoDataChildparameter, in order to avoid to check for thesnapshot.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.