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
AppState
class implementing theAppStateModel
interface, holds the state of the app, here is where the only two BLoCsTodosBloc
andStatsBloc
are 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
,DetailScreen
andAddEditScreen
). - To send data from the
TodosBloc
to theStatsBloc
, in order to calculate the number of active and completed todos, it is used thesend
method of theListSender
class. First, in theTodosBloc
is created an instance of theListSender
class, then by its methodsetReceiver
, in theAppState
class is set a reference to the stream on theStatsBloc
that will receive the todos list whenever the methodsend
is called. In this case, there is no need for the two BLoCs to share the same source of data, or having to pass theTodosBloc
as a parameter to theStatsBloc
to 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
ValueBuilder
widget, 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 theStreamBuilder
and adds some callbacks to handle the stream state and return aContainer
if no widget is passed to theNoDataChild
parameter, in order to avoid to check for thesnapshot.hasData
property 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.