Viewing version 0.7.7. Open the latest package page.

bloc

0.7.7

The goal of this package is to make it easy to implement the BLoC (Business Logic Component) design pattern.

Download archive

Readme

Build Status codecov Pub Awesome Flutter License: MIT Gitter


A dart package that helps implement the Bloc pattern.

This package is built to work with RxDart.dart 0.18.1+.

Overview

The goal of this package is to make it easy to implement the Bloc Design Pattern (Business Logic Component).

This design pattern helps to separate presentation from business logic. Following the Bloc pattern facilitates testability and reusability. This package abstracts reactive aspects of the pattern allowing developers to focus on converting events into states.

Glossary

Events are the input to a Bloc. They are commonly UI events such as button presses. Events are dispatched and then converted to States.

States are the output of a Bloc. Presentation components can listen to the stream of states and redraw portions of themselves based on the given state (see BlocBuilder for more details).

Transitions occur when an Event is dispatched after mapEventToState has been called but before the Bloc's state has been updated. A Transition consists of the currentState, the event which was dispatched, and the nextState.

BlocSupervisor oversees Blocs and delegates to BlocDelegate.

BlocDelegate handles events from all Blocs which are delegated by the BlocSupervisor. Can be used to observe all Bloc Transitions. It is a great way to handle logging/analytics universally.

Bloc Interface

initialState is the state before any events have been processed (before mapEventToState has ever been called). initialState must be implemented.

mapEventToState is a method that must be implemented when a class extends Bloc. The function takes two arguments: state and event. mapEventToState is called whenever an event is dispatched by the presentation layer. mapEventToState must convert that event, along with the current state, into a new state and return the new state in the form of a Stream which is consumed by the presentation layer.

dispatch is a method that takes an event and triggers mapEventToState. dispatch may be called from the presentation layer or from within the Bloc (see examples) and notifies the Bloc of a new event.

transform is a method that can be overridden to transform the Stream<Event> before mapEventToState is called. This allows for operations like distinct() and debounce() to be used.

onTransition is a method that can be overridden to handle whenever a Transition occurs. A Transition occurs when a new Event is dispatched and mapEventToState is called. onTransition is called before a Bloc's state has been updated. It is a great place to add bloc-specific logging/analytics.

BlocDelegate Interface

onTransition is a method that can be implemented to handle whenever a Transition occurs from any Bloc. It is a great place to add universal logging/analytics.

Usage

For simplicity we can create a Bloc that always returns a stream of static strings in response to any event. That would look something like:

class SimpleBloc extends Bloc<dynamic, String> {
  @override
  String get initialState => '';

  @override
  Stream<String> mapEventToState(String currentState, dynamic event) async* {
    yield 'data';
  }
}

That isn't a very realistic use-case so let's take something more practical like a login flow.

We're going to need to define what our different LoginStates are going to be. For simplicity, let's say we only have 4 states:

  • initial
  • loading
  • failure
  • success
class LoginState {
  final bool isLoading;
  final bool isLoginButtonEnabled;
  final String error;
  final String token;

  const LoginState({
    @required this.isLoading,
    @required this.isLoginButtonEnabled,
    @required this.error,
    @required this.token,
  });

  factory LoginState.initial() {
    return LoginState(
      isLoading: false,
      isLoginButtonEnabled: true,
      error: '',
      token: '',
    );
  }

  factory LoginState.loading() {
    return LoginState(
      isLoading: true,
      isLoginButtonEnabled: false,
      error: '',
      token: '',
    );
  }

  factory LoginState.failure(String error) {
    return LoginState(
      isLoading: false,
      isLoginButtonEnabled: true,
      error: error,
      token: '',
    );
  }

  factory LoginState.success(String token) {
    return LoginState(
      isLoading: false,
      isLoginButtonEnabled: true,
      error: '',
      token: token,
    );
  }
}

Next we need to define the different events that our Bloc will respond to. Again, for simplicity, let's say there is just a single event we will handle: LoginButtonPressed.

abstract class LoginEvent {}

class LoginButtonPressed extends LoginEvent {
  final String username;
  final String password;

  LoginButtonPressed({@required this.username, @required this.password});
}

Now that we've identified our states and events, our LoginBloc should look something like:

class LoginBloc extends Bloc<LoginEvent, LoginState> {
  LoginState get initialState => LoginState.initial();

  void onLoginButtonPressed({String username, String password}) {
    dispatch(
      LoginButtonPressed(
        username: username,
        password: password,
      ),
    );
  }

  @override
  Stream<LoginState> mapEventToState(LoginState currentState, LoginEvent event) async* {
    if (event is LoginButtonPressed) {
      yield LoginState.loading();

      try {
        final token = await _authenticate(event.username, event.password);
        yield LoginState.success(token);
      } catch (error) {
        yield LoginState.failure(error.toString());
      }
    }
  }
}

As our app grows and relies on multiple Blocs, it becomes useful to see the Transitions for all Blocs. This can easily be achieved by implementing a BlocDelegate.

class SimpleBlocDelegate implements BlocDelegate {
  @override
  void onTransition(Transition transition) {
    print(transition.toString());
  }
}

Now that we have our SimpleBlocDelegate, we just need to tell the BlocSupervisor to use our delegate in our main.dart.

void main() {
  BlocSupervisor().delegate = SimpleBlocDelegate();
}

At this point, all Bloc Transitions will be reported to the SimpleBlocDelegate.

Dart Versions

  • Dart 2: >= 2.0.0

Examples

Contributors

Changelog

0.1.0

Initial Version of the library.

  • Includes the ability to create a custom Bloc by extending Bloc class.
  • Includes the ability to connect presentation layer to Bloc by using the BlocBuilder Widget.

0.1.1

Minor Updates to Documentation.

0.1.2

Additional Minor Updates to Documentation.

0.2.0

Added Support for Stream Transformation

  • Includes Stream<E> transform(Stream<E> events)
  • Updates to Documentation

0.2.1

Minor Updates to Documentation.

0.2.2

Additional Minor Updates to Documentation.

0.2.3

Additional Minor Updates to Documentation.

0.2.4

Additional Minor Updates to Documentation.

0.2.5

Additional Minor Updates to Documentation.

0.3.0

Updated mapEventToState to take current state as an argument.

  • Stream<S> mapEventToState(E event) -> Stream<S> mapEventToState(S state, E event)
  • Updates to Documentation.
  • Updates to Example.

0.4.0

Added BlocProvider.

  • BlocProvider.of(context)
  • Updates to Documentation.
  • Updates to Example.

0.4.1

Minor Updates to Documentation.

0.4.2

Additional minor Updates to Documentation.

0.5.0

Moved Flutter Widgets to flutter_bloc package

0.5.1

Minor Updates to Documentation

0.5.2

Additional minor Updates to Documentation.

0.6.0

Transitions and initialState updates.

  • Added Transitions and onTransition
  • Made initialState required

0.7.0

Added BlocSupervisor and BlocDelegate.

  • BlocSupervisor notifies BlocDelegate of Transitions
  • BlocDelegate exposes onTransition which is invoked for all Bloc Transitions.

0.7.1

Improvements to Bloc usage in pure Dart applications.

  • Bloc state is seeded with initialState automatically

0.7.2

Transition Fix

  • Bloc with mapEventToState which returns multiple states per event will now correctly report the Transitions.

0.7.3

Minor Updates to Documentation

0.7.4

Updated mapEventToState parameter name

  • Stream<S> mapEventToState(S state, E event) -> Stream<S> mapEventToState(S currentState, E event)
  • Updates to Documentation.
  • Updates to Example.

0.7.5

Exposed currentState in Bloc

  • Updates to Documentation.

0.7.6

Minor Updates to Documentation

0.7.7

Additional Minor Updates to Documentation

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'bloc:0.7.7'
pubspec.yaml
name: bloc
description: The goal of this package is to make it easy to implement the BLoC (Business Logic Component) design pattern.
version: 0.7.7
author: felix.angelov <felangelov@gmail.com>
homepage: https://felangel.github.io/bloc

environment:
  sdk: ">=2.0.0-dev.28.0 <3.0.0"

dependencies:
  meta: ^1.1.6
  rxdart: ">=0.18.1 <1.0.0"

dev_dependencies:
  test: ">=1.3.0 <2.0.0"
  test_coverage: ^0.2.0
  mockito: ^4.0.0

Version history

VersionPublishedArchiveSHA-256
0.1.0Cached · Upstream SHA-256 matchedDownloadbfe4b4a2…724c57
0.1.1Cached · Upstream SHA-256 matchedDownload86193bc3…e3b0fa
0.1.2Cached · Upstream SHA-256 matchedDownload5f3eaad8…a2abad
0.2.0Cached · Upstream SHA-256 matchedDownloade16fd926…2ed9c4
0.2.1Cached · Upstream SHA-256 matchedDownloadb56bf959…942cc5
0.2.2Cached · Upstream SHA-256 matchedDownloadfcc7a7cc…6b2af7
0.2.3Cached · Upstream SHA-256 matchedDownload9e35b326…dafd48
0.2.4Cached · Upstream SHA-256 matchedDownload1231c70c…4c85b3
0.2.5Cached · Upstream SHA-256 matchedDownloadb805a9e0…1a0977
0.3.0Cached · Upstream SHA-256 matchedDownload3699626f…482ae3
0.4.0Cached · Upstream SHA-256 matchedDownload77d3150d…6bddd5
0.4.1Cached · Upstream SHA-256 matchedDownload83ca5fac…7a91ce
0.4.2Cached · Upstream SHA-256 matchedDownload53dc13e2…9fc2ef
0.5.0Cached · Upstream SHA-256 matchedDownload3fbb3f16…d502ca
0.5.1Cached · Upstream SHA-256 matchedDownload2a89d793…b470fb
0.5.2Cached · Upstream SHA-256 matchedDownload552344cd…1d6ab4
0.6.0Cached · Upstream SHA-256 matchedDownload45eddbfa…fe8874
0.7.0Cached · Upstream SHA-256 matchedDownload41e0f5bc…d7cba4
0.7.1Cached · Upstream SHA-256 matchedDownloadb5acda46…c78e71
0.7.2Cached · Upstream SHA-256 matchedDownloadb9b0dfbb…4785f4
0.7.3Cached · Upstream SHA-256 matchedDownloadc159b538…b53225
0.7.4Cached · Upstream SHA-256 matchedDownload1ebcf265…c50c26
0.7.5Cached · Upstream SHA-256 matchedDownload1ab88a6d…f01790
0.7.6Cached · Upstream SHA-256 matchedDownload2dc55af6…f57cf9
0.7.7Cached · Upstream SHA-256 matchedDownload2ea3af00…f4a789
0.7.8Cached · Upstream SHA-256 matchedDownload88673fa0…01d8af
0.8.0Cached · Upstream SHA-256 matchedDownload607e8450…bf202d
0.8.1Cached · Upstream SHA-256 matchedDownloadc495b55a…f41e07
0.8.2Cached · Upstream SHA-256 matchedDownloadc2b3a781…2f7f8a
0.8.3Cached · Upstream SHA-256 matchedDownload7486fa77…a18588
0.8.4Cached · Upstream SHA-256 matchedDownloadc653498b…57bfa1
0.9.0Cached · Upstream SHA-256 matchedDownloadf5ddef39…b23d5d
0.9.1Cached · Upstream SHA-256 matchedDownload903bb05d…67ac8f
0.9.2Cached · Upstream SHA-256 matchedDownload3b35b9fb…a170fd
0.9.3Cached · Upstream SHA-256 matchedDownload092530d5…03b8b2
0.9.4Cached · Upstream SHA-256 matchedDownload32ec9ec6…b81639
0.9.5Cached · Upstream SHA-256 matchedDownloadc95dc63e…25cd90
0.10.0Cached · Upstream SHA-256 matchedDownload35a83cde…96fdc4
0.11.0Cached · Upstream SHA-256 matchedDownload0a0a388b…8a0e85
0.11.1Cached · Upstream SHA-256 matchedDownload499e942f…89cce8
0.11.2Cached · Upstream SHA-256 matchedDownloaddd9ea891…987395
0.12.0Cached · Upstream SHA-256 matchedDownload870bb53d…09ef1b
0.13.0Cached · Upstream SHA-256 matchedDownloadcfcc09c3…19ba3c
0.14.0Cached · Upstream SHA-256 matchedDownloadfc65c2e2…e13393
0.14.1Cached · Upstream SHA-256 matchedDownloadaf2ea6e1…09681c
0.14.2Cached · Upstream SHA-256 matchedDownload8f89f2e1…03c797
0.14.3Cached · Upstream SHA-256 matchedDownloadcb54ce33…1790c7
0.14.4Cached · Upstream SHA-256 matchedDownloada2971321…9af79c
0.15.0Cached · Upstream SHA-256 matchedDownload4dc615b1…e49e76
0.16.0Cached · Upstream SHA-256 matchedDownload4a9df2c4…c63dfd
0.16.1Cached · Upstream SHA-256 matchedDownloadd87523ac…a6635f
1.0.0Cached · Upstream SHA-256 matchedDownload0d57df01…063f4a
1.0.1Cached · Upstream SHA-256 matchedDownloadb2592b70…f07cdc
2.0.0Cached · Upstream SHA-256 matchedDownload8544ef60…ec3828
3.0.0-dev.1Cached · Upstream SHA-256 matchedDownload9c23c45e…f14457
3.0.0Cached · Upstream SHA-256 matchedDownloadccc49d51…61ada4
4.0.0-dev.1Cached · Upstream SHA-256 matchedDownload597621d1…62e462
4.0.0-dev.2Cached · Upstream SHA-256 matchedDownloade21f7cfb…960bc8
4.0.0-dev.3Cached · Upstream SHA-256 matchedDownload641593fe…d90033
4.0.0-dev.4Cached · Upstream SHA-256 matchedDownload92820ef4…99d334
4.0.0Cached · Upstream SHA-256 matchedDownloadbc0e8756…bac75a
5.0.0-dev.1Cached · Upstream SHA-256 matchedDownload488a1a1f…9d0d2e
5.0.0-dev.2Cached · Upstream SHA-256 matchedDownload2720922c…c03302
5.0.0-dev.3Cached · Upstream SHA-256 matchedDownload8d2480cf…e4f067
5.0.0-dev.4Cached · Upstream SHA-256 matchedDownload3edd01b5…db4dd9
5.0.0-dev.5Cached · Upstream SHA-256 matchedDownload6639b001…7a8dac
5.0.0-dev.6Cached · Upstream SHA-256 matchedDownload5293933b…a20eb5
5.0.0-dev.7Cached · Upstream SHA-256 matchedDownload21456eff…2a4b1e
5.0.0-dev.8Cached · Upstream SHA-256 matchedDownload8b8a74e0…0b8435
5.0.0-dev.9Cached · Upstream SHA-256 matchedDownloadd0af89ca…0ebd1d
5.0.0-dev.10Cached · Upstream SHA-256 matchedDownloadd6513650…a4f00e
5.0.0-dev.11Cached · Upstream SHA-256 matchedDownload96f45d25…c4d29b
5.0.0Cached · Upstream SHA-256 matchedDownload66e5e429…5b9754
5.0.1Cached · Upstream SHA-256 matchedDownload872e0786…f433d4
6.0.0-dev.1Cached · Upstream SHA-256 matchedDownloaddde227e1…8a7beb
6.0.0-dev.2Cached · Upstream SHA-256 matchedDownload0a907ea1…b188e8
6.0.0Cached · Upstream SHA-256 matchedDownload9bba4d00…9dddf5
6.0.1Cached · Upstream SHA-256 matchedDownload28f8ae0e…c0e167
6.0.2Cached · Upstream SHA-256 matchedDownloadda311fd0…9058c3
6.0.3Cached · Upstream SHA-256 matchedDownloade4a4e6b2…7ac1fa
6.1.0Cached · Upstream SHA-256 matchedDownload527c4923…233ea2
6.1.1Cached · Upstream SHA-256 matchedDownloadd6e1fb79…6ab57e
6.1.2Cached · Upstream SHA-256 matchedDownload7108b980…aa60f4
6.1.3Cached · Upstream SHA-256 matchedDownload428a6baa…bc826b
7.0.0-nullsafety.0Cached · Upstream SHA-256 matchedDownloadd9d8220e…554ea5
7.0.0-nullsafety.1Cached · Upstream SHA-256 matchedDownload1c277595…a8344d
7.0.0-nullsafety.2Cached · Upstream SHA-256 matchedDownload2c6e21d5…0b9875
7.0.0-nullsafety.3Cached · Upstream SHA-256 matchedDownload239c91fb…fc7d9c
7.0.0-nullsafety.4Cached · Upstream SHA-256 matchedDownload564071b0…4c0c64
7.0.0Cached · Upstream SHA-256 matchedDownload8fb2e405…5f1026
7.1.0Cached · Upstream SHA-256 matchedDownload2ce4a471…1a4a13
7.2.0-dev.1Cached · Upstream SHA-256 matchedDownload3a583482…35aa1f
7.2.0-dev.2Cached · Upstream SHA-256 matchedDownloadf3e36406…3dc0e9
7.2.0-dev.3Cached · Upstream SHA-256 matchedDownload9cd52452…65b6d4
7.2.0Cached · Upstream SHA-256 matchedDownload250bf997…439333
7.2.1Cached · Upstream SHA-256 matchedDownload6f1b87b6…a516bd
8.0.0-dev.1Cached · Upstream SHA-256 matchedDownload37f9b6cd…6a6866
8.0.0-dev.2Cached · Upstream SHA-256 matchedDownloadd6e6c205…b0c7a9
8.0.0-dev.3Cached · Upstream SHA-256 matchedDownloadb64573ee…e985bd
8.0.0-dev.4Cached · Upstream SHA-256 matchedDownloadc21e5efa…9f82e0
8.0.0-dev.5Cached · Upstream SHA-256 matchedDownloadd489783d…cc0b16
8.0.0Cached · Upstream SHA-256 matchedDownloada9bd93ba…8baae3
8.0.1Cached · Upstream SHA-256 matchedDownloadc12099a6…9dd7e0
8.0.2Cached · Upstream SHA-256 matchedDownload104c7223…98f32d
8.0.3Cached · Upstream SHA-256 matchedDownload318e6cc6…b88e03
8.1.0Cached · Upstream SHA-256 matchedDownloadbd4f8027…44534a
8.1.1Cached · Upstream SHA-256 matchedDownload658a5ae5…e11a80
8.1.2Cached · Upstream SHA-256 matchedDownload3820f15f…be5b49
8.1.3Cached · Upstream SHA-256 matchedDownloadf53a110e…a2db9e
8.1.4Cached · Upstream SHA-256 matchedDownload106842ad…77804e
9.0.0Cached · Upstream SHA-256 matchedDownload52c10575…6ac189
9.0.1Cached · Upstream SHA-256 matchedDownloade18b8e78…994d78
9.1.0Cached · Upstream SHA-256 matchedDownloada2cebb89…74537d
9.2.0Cached · Upstream SHA-256 matchedDownloada48653a8…7efb81
9.2.1Cached · Upstream SHA-256 matchedDownloade03b2359…b75413

Dependencies

meta "^1.1.6"

rxdart "\u003e=0.18.1 \u003c1.0.0"