flutter-bloc-patterns
flutter-bloc-patterns copied to clipboard
A set of most common BLoC use cases built on top of flutter_bloc library
Flutter BLoC patterns
A set of most common BLoC use cases build on top flutter_bloc library.
Key contepts
BLoC
BLoC, aka Business Logic Component, is a state management system for Flutter. It's main goal is to separate business logic from the presentation layer. The BLoC handles user actions or any other events and generates new state for the view to render.
Repository
A Repository to handles data operations. It knows where to get the data from and what API calls to make when data is updated. A Repository can utilize a single data source as well as it can be a mediator between different data sources, such as database, web services and caches.
ViewStateBuilder
ViewStateBuilder is responsible for building the UI based on the view state. It's a wrapper over the BlocBuilder widget so it accepts a bloc object and a set of handy callbacks, which corresponds to each possible state:
onReady- informs the presentation layer that view is in it's initial state, and no action has taken place yet,onLoading- informs the presentation layer that the data is being loaded, so it can display a loading indicator,onRefreshing- informs the presentation layer that the data is being refreshed, so it can display a refresh indicator or/and the current state of list items,onSuccess- informs the presentation layer that the loading is completed and anonnulland not empty data was retrieved,onEmpty- informs the presentation layer that the loading is completed, butnullor empty data was retrieved,onError- informs the presentation layer that the loading or refreshing has ended with an error. It also provides an error that has occurred.
ViewStateListener
ViewStateListener is responsible for performing an action based on the view state. It should be used for functionality that needs to occur only in response to a state change such as navigation, showing a SnackBar etc. ViewStateListener is a wrapper over the BlocListener widget so it accepts a bloc object as well as a child widget and a set of handy callbacks corresponding to a given state:
onLoading- informs the presentation layer that the data is being loaded,onRefreshing- informs the presentation layer that the data is being refreshed,onSuccess- informs the presentation layer that the loading is completed and anonnulland not empty data was retrieved,onEmpty- informs the presentation layer that the loading is completed, butnullor empty data was retrieved,onError- informs the presentation layer that the loading or refreshing has ended with an error. It also provides an error that has occurred.
Features
ListBloc
The most basic use case. Allows to fetch, refresh and display a list of items without filtering and pagination. Thus, ListBloc should be used only with a reasonable amount of data. ListBloc provides the methods for loading and refreshing data: loaditems() and refreshitems(). The former is most suitable for initial data fetch or for retry action when the first fetch fails. The latter is designed for being called after the initial fetch succeeds. It can be performed when the list has already been loaded. To display the current view state ListBloc cooperates with BlocBuilder as well as ViewStateBuilder.
ListRepository
A ListRepository implementation should provide only one method:
Future<List<T>> getAll(); - this method is responsible for providing all the data to the ListBloc.
Where:
Tis the item type returned by this repository.
Usage
List BLoC Sample App
FilterListBloc
An extension to the ListBloc that allows filtering.
FilterRepository
FilterListRepository provides two methods:
Future<List<T>> getAll(); - this method is called when a null filter is provided and should return all items,
Future<List<T>> getBy(F filter); - this method is called with nonnull filter and should return only items that match it.
Where:
Tis the item type returned by this repository,Fis the filter type, which can be primitive as well as complex object.
Usage
Filter List BLoC Sample App
PagedListBloc
A list BLoC with pagination but without filtering. It works best with Infinite Widgets but a custom presentation layer can provided as well.
Page
Contains information about the current page, this is number and size.
PagedList
List of items with information if there are more items or not.
PagedListRepository
PagedListRepository comes with only one method:
Future<List<T>> getAll(Page page); - this method retrieves items meeting the pagination restriction provided by the page object.
When items are exceeded it should return an empty list or throw PageNotFoundException. PagedListBloc will handle both cases in the same way.
Where:
Tis the item type returned by this repository.
Usage
Paged List BLoC Sample App
PagedListFilterBloc
A list BLoC with pagination and filtering. It works best with Infinite Widgets but a custom presentation layer can provided as well.
Page
Contains information about the current page, this is number and size.
PagedList
List of items with information if there are more items or not.
PagedListFilterRepository
PagedListFilterRepository provides only two methods:
Future<List<T>> getAll(Page page); - retrieves items meeting the pagination restriction provided by the page object.
Future<List<T>> getBy(Page page, F filter); - retrieves items meeting pagination as well as the filter restrictions provided by the page and filter objects.
When items are exceeded it should return an empty list or throw PageNotFoundException. PagedListFilterBloc will handle both cases in the same way.
Where:
Tis the item type returned by this repository,Fis the filter type, which can be primitive as well as complex object.
Usage
Paged List BLoC Sample App
DetailsBloc
A BLoC that allows to fetch a single item with given identifier.
DetailsRepository
DetailsRepository comes with only one method:
Future<T> getById(I id); - this method retrieves an item with given id. When there's no item matching the id the null should be returned or itemNotFoundException should be thrown. In both cases the DetailsBloc will emit Empty state.
Where:
Tis the item type returned by this repository,Iis the id type, it can be primitive as well as a complex object.
Usage:
List/Details BLoC Sample App
Dart version
- Dart 2: >= 2.12.0