Viewing version 0.2.3. Open the latest package page.

bloc

0.2.3

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

Download archive

Readme

Build Status codecov Pub


A dart package that helps implement the Bloc pattern.

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

Bloc

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).

Bloc Interface

mapEventToState is a method that must be implemented when a class extends Bloc. The function takes a single argument, event. mapEventToState is called whenever an event is dispatched by the presentation layer. mapEventToState must convert that event into a state and return the state in the form of a Stream so that it can be consumed by the presentation layer.

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

initialState is the state before any events have been processed (before mapEventToState has ever been called). initialState is an optional getter. If unimplemented, initialState will be null.

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.

Bloc Widgets

BlocBuilder is a Flutter widget which requires a Bloc and a builder function. BlocBuilder handles building the widget in response to new states. BlocBuilder is very similar to StreamBuilder but has a more simple API to reduce the amount of boilerplate code needed.

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
  Stream<String> mapEventToState(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(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.error(error.toString());
      }
    }
  }
}

Now that we have the LoginBloc lets take a look at how to use BlocBuilder to hook up our LoginForm widget to our LoginBloc.

  class LoginForm extends StatelessWidget {
    final LoginBloc loginBloc;
    final usernameController = TextEditingController();
    final passwordController = TextEditingController();

    const LoginForm({Key key, @required this.loginBloc}): super(key: key);

    @override
    Widget build(BuildContext context) {
      return BlocBuilder<LoginState>(
        bloc: loginBloc,
        builder: (
          BuildContext context,
          LoginState loginState,
        ) {
          if (loginState.token.isNotEmpty) {
            // user is authenticated do something...
          }

          return Form(
            child: Column(
              children: [
                Text(
                  loginState.error,
                ),
                TextFormField(
                  decoration: InputDecoration(labelText: 'username'),
                  controller: usernameController,
                ),
                TextFormField(
                  decoration: InputDecoration(labelText: 'password'),
                  controller: passwordController,
                  obscureText: true,
                ),
                RaisedButton(
                  onPressed: loginState.isLoginButtonEnabled
                      ? _onLoginButtonPressed
                      : null,
                  child: Text('Login'),
                ),
                Container(
                  child:
                      loginState.isLoading ? CircularProgressIndicator() : null,
                ),
              ],
            ),
          );
        },
      );
    }

    _onLoginButtonPressed() {
      loginBloc.onLoginButtonPressed(
        username: usernameController.text,
        password: passwordController.text,
      );
    }
  }

At this point we have sucessfully separated our presentational layer from our business logic layer. Notice that the LoginForm widget knows nothing about what happens when a user taps the button. The form simply tells the LoginBloc that the user has pressed the button via dispatch. From that point, the LoginBloc tells the LoginForm to be in the loading state and proceeds to authenticate the user. If the user is successfully authenticated, the LoginBloc tells the LoginForm to be in the LoginSuccess state. If authentication failed, the LoginBloc tells the LoginForm to be in the LoginError state.

Dart Versions

  • Dart 2: >= 2.0.0

Examples

  • Simple Theme Example - an example of how to create a ThemeBloc to manage dynamically changing the theme of your flutter app.

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.

Installing

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

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

dependencies:
  rxdart: ">=0.18.1 <1.0.0"
  flutter:
    sdk: flutter

dev_dependencies:
  flutter_test:
    sdk: flutter
  test: ">=1.3.0 <2.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

flutter { "sdk": "flutter" }

rxdart "\u003e=0.18.1 \u003c1.0.0"