Giter Site home page Giter Site logo

klisiewicz / flutter-bloc-patterns Goto Github PK

View Code? Open in Web Editor NEW
237.0 1.0 25.0 254 KB

A set of most common BLoC use cases built on top of flutter_bloc library

License: MIT License

Dart 99.47% Kotlin 0.12% Swift 0.37% Objective-C 0.03%
flutter dart dartlang flutter-package bloc flutter-bloc flutter-bloc-pattern state-management library flutter-bloc-patterns

flutter-bloc-patterns's Introduction

Flutter BLoC patterns

Codemagic build status Star on GitHub License: MIT


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:

  • initial - informs the presentation layer that view is in it's initial state, and no action has taken place yet,
  • loading - informs the presentation layer that the data is being loaded, so it can display a loading indicator,
  • refreshing - 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,
  • data - informs the presentation layer that the loading is completed and a nonnull and not empty data was retrieved,
  • empty - informs the presentation layer that the loading is completed, but null or empty data was retrieved,
  • error - 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,
  • onData - informs the presentation layer that the loading is completed and a nonnull and not empty data was retrieved,
  • onEmpty - informs the presentation layer that the loading is completed, but null or 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() - most suitable for initial data fetch or for retry action when the first fetch fails,
  • refreshitems() - designed for being called after the initial fetch succeeds.

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:

  • T is 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:

  • T is the item type returned by this repository,
  • F is 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 be 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:

  • T is 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 be 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:

  • T is the item type returned by this repository,
  • F is 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. In this cases the DetailsBloc will emit Empty state.

Where:

  • T is the item type returned by this repository,
  • I is the id type, it can be primitive as well as a complex object.

Usage:

List/Details BLoC Sample App

ConnectionBloc

A BLoC that exposes the Internet connection state to the UI.

Connection

The Internet connection state. It can be either online or offline.

ConnectionRepository

ConnectionRepository notifies about connection state changes, such as going online or offline.

Please notice, that this is only a contract and a developer needs to provide an implementation. This can be done using one of many popular packages, like:

Or whatever works for you. A sample implementation using connectivity_plus may look as follows:

class ConnectivityPlusRepository implements ConnectionRepository {
  @override
  Stream<Connection> observe() {
    // Required due to https://github.com/fluttercommunity/plus_plugins/issues/2527
    return MergeStream([
      Stream.fromFuture(_connectivity.checkConnectivity()),
      _connectivity.onConnectivityChanged,
    ]).map(
          (ConnectivityResult result) => result != ConnectivityResult.none
          ? Connection.online
          : Connection.offline,
    );
  }
}

ConnectionBuilder

ConnectionBuilder is responsible for building the UI based Connection state.

It's a wrapper over the BlocBuilder widget so it accepts a bloc object and provides WidgetBuilder functions for possible states:

  • online - a builder for the the Connection.online state,
  • offline - a builder for the the Connection.offline state,

ConnectionListener

ConnectionListener is responsible for performing a one-time action based on the Connection state change.

It should be used for functionality that needs to occur only once in response to the Connection state change such as navigation, SnackBar, showing a Dialog, etc.

ConnectionListener is a wrapper over the BlocListener widget so it accepts a bloc object as well as a child widget. It also takes ConnectionCallback functions for possible states:

  • onOnline - a callback for the the Connection.online state,
  • onOffline - a callback for the Connection.offline state.

Usage:

Connection Sample App

Dart version

  • Dart 3: >= 3.0.0

Author

flutter-bloc-patterns's People

Contributors

klisiewicz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

flutter-bloc-patterns's Issues

+ may fetch repeatable item, it is need check

Stream<ViewState> _emitNextPageLoaded(
    Page page,
    List<T> pageElements,
  ) async* {
    final List<T> allElements = _currentElements + pageElements;
    yield Success(
      PagedList<T>(allElements, hasReachedMax: page.size > pageElements.length),
    );
  }
for (T item in source) {
          if (!this.contains(item) && hasMore) this.add(item);
}

What's the best way to extend a Bloc?

Hi everyone, I'm playing with the package and I found the necessity to extend a Bloc. While I'm in a detail page I can edit the selected item but by design DetailsEvent is sealed so if I try to extends an event from it and put it in a on instruction it doesn't work. I know I can make another Bloc to handle every other operation but I would love to know if there is any way to do this with the current bloc or if it will be supported!

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.