bloc 0.9.4

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 99
Bloc Package

Build Status codecov Pub Flutter.io Awesome Flutter Star on GitHub Gitter License: MIT


A dart package that helps implement the BLoC pattern.

This package is built to work with flutter_bloc and angular_bloc.

Overview #

Bloc Architecture

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 intercept all Bloc Transitions and all Bloc errors. It is a great way to handle logging/analytics as well as error handling 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.

onError is a method that can be overridden to handle whenever an Exception is thrown. By default all exceptions will be ignored and Bloc functionality will be unaffected. It is a great place to add bloc-specific error handling.

BlocDelegate Interface #

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

onError is a method that can be overriden to handle whenever an Exception is thrown from any Bloc. It is a great place to add universal error handling.

Usage #

For simplicity we can create a CounterBloc like:

class CounterBloc extends Bloc<CounterEvent, int> {
  @override
  int get initialState => 0;

  @override
  Stream<int> mapEventToState(int currentState, CounterEvent event) async* {
    switch (event) {
      case CounterEvent.decrement:
        yield currentState - 1;
        break;
      case CounterEvent.increment:
        yield currentState + 1;
        break;
    }
  }
}

Our CounterBloc converts CounterEvents to integers.

As a result, we need to define our CounterEvent like:

enum CounterEvent { increment, decrement }

Then we can dispatch events to our bloc like so:

void main() {
  final counterBloc = CounterBloc();

  counterBloc.dispatch(CounterEvent.increment);
  counterBloc.dispatch(CounterEvent.increment);
  counterBloc.dispatch(CounterEvent.increment);

  counterBloc.dispatch(CounterEvent.decrement);
  counterBloc.dispatch(CounterEvent.decrement);
  counterBloc.dispatch(CounterEvent.decrement);
}

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 extends BlocDelegate {
  @override
  void onTransition(Transition transition) {
    print(transition);
  }
}

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();

  final counterBloc = CounterBloc();

  counterBloc.dispatch(CounterEvent.increment); // { currentState: 0, event: CounterEvent.increment, nextState: 1 }
  counterBloc.dispatch(CounterEvent.increment); // { currentState: 1, event: CounterEvent.increment, nextState: 2 }
  counterBloc.dispatch(CounterEvent.increment); // { currentState: 2, event: CounterEvent.increment, nextState: 3 }

  counterBloc.dispatch(CounterEvent.decrement); // { currentState: 3, event: CounterEvent.decrement, nextState: 2 }
  counterBloc.dispatch(CounterEvent.decrement); // { currentState: 2, event: CounterEvent.decrement, nextState: 1 }
  counterBloc.dispatch(CounterEvent.decrement); // { currentState: 1, event: CounterEvent.decrement, nextState: 0 }
}

At this point, all Bloc Transitions will be reported to the SimpleBlocDelegate and we can see them in the console after running our app.

If we want to be able to handle any Exceptions that might be thrown in mapEventToState we can also override onError in our SimpleBlocDelegate.

class SimpleBlocDelegate extends BlocDelegate {
  @override
  void onTransition(Transition transition) {
    print(transition);
  }

  @override
  void onError(Object error, StackTrace stacktrace) {
    print('$error, $stacktrace');
  }
}

At this point, all Bloc exceptions will also be reported to the SimpleBlocDelegate and we can see them in the console.

Dart Versions #

  • Dart 2: >= 2.0.0

Examples #

  • Counter - an example of how to create a CounterBloc in a pure Dart app.

Maintainers #

0.9.4 #

Calls to dispatch after after dispose has been called trigger onError in the Bloc and BlocDelegate.

0.9.3 #

Restrict rxdart to ">=0.18.1 <0.21.0" due to breaking changes.

0.9.2 #

Additional Minor Updates to Documentation

0.9.1 #

Minor Updates to Documentation

0.9.0 #

Bloc and BlocDelegate Error Handling

  • Added onError to Bloc for local error handling.
  • Added onError to BlocDelegate for global error handling.

0.8.4 #

Blocs handle exceptions thrown in mapEventToState and documentation updates.

0.8.3 #

Minor Internal Improvements and Documentation Updates

0.8.2 #

Additional Minor Updates to Documentation

0.8.1 #

Minor Updates to Documentation

0.8.0 #

Blocs ignore duplicate states

0.7.8 #

Additional Minor Updates to Documentation

0.7.7 #

Additional Minor Updates to Documentation

0.7.6 #

Minor Updates to Documentation

0.7.5 #

Exposed currentState in Bloc

  • 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.3 #

Minor Updates to Documentation

0.7.2 #

Transition Fix

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

0.7.1 #

Improvements to Bloc usage in pure Dart applications.

  • Bloc state is seeded with initialState automatically

0.7.0 #

Added BlocSupervisor and BlocDelegate.

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

0.6.0 #

Transitions and initialState updates.

  • Added Transitions and onTransition
  • Made initialState required

0.5.2 #

Additional minor Updates to Documentation.

0.5.1 #

Minor Updates to Documentation

0.5.0 #

Moved Flutter Widgets to flutter_bloc package

0.4.2 #

Additional minor Updates to Documentation.

0.4.1 #

Minor Updates to Documentation.

0.4.0 #

Added BlocProvider.

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

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.2.5 #

Additional Minor Updates to Documentation.

0.2.4 #

Additional Minor Updates to Documentation.

0.2.3 #

Additional Minor Updates to Documentation.

0.2.2 #

Additional Minor Updates to Documentation.

0.2.1 #

Minor Updates to Documentation.

0.2.0 #

Added Support for Stream Transformation

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

0.1.2 #

Additional Minor Updates to Documentation.

0.1.1 #

Minor Updates to Documentation.

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.

example/main.dart

import 'dart:async';

import 'package:bloc/bloc.dart';

enum CounterEvent { increment, decrement }

class CounterBloc extends Bloc<CounterEvent, int> {
  @override
  int get initialState => 0;

  @override
  Stream<int> mapEventToState(int currentState, CounterEvent event) async* {
    switch (event) {
      case CounterEvent.decrement:
        // Simulating Network Latency
        await Future<void>.delayed(Duration(seconds: 1));
        yield currentState - 1;
        break;
      case CounterEvent.increment:
        // Simulating Network Latency
        await Future<void>.delayed(Duration(milliseconds: 500));
        yield currentState + 1;
        break;
      default:
        throw Exception('unhandled event: $event');
    }
  }
}

class SimpleBlocDelegate extends BlocDelegate {
  @override
  void onTransition(Transition transition) {
    print(transition);
  }

  @override
  void onError(Object error, StackTrace stacktrace) {
    print(error);
  }
}

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

  final counterBloc = CounterBloc();

  counterBloc.dispatch(CounterEvent.increment);
  counterBloc.dispatch(CounterEvent.increment);
  counterBloc.dispatch(CounterEvent.increment);

  counterBloc.dispatch(CounterEvent.decrement);
  counterBloc.dispatch(CounterEvent.decrement);
  counterBloc.dispatch(CounterEvent.decrement);

  counterBloc.dispatch(null); // Triggers Exception

  // The exception triggers `SimpleBlocDelegate.onError` but does not impact bloc functionality.
  counterBloc.dispatch(CounterEvent.increment);
  counterBloc.dispatch(CounterEvent.decrement);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  bloc: ^0.9.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:bloc/bloc.dart';
  
Version Uploaded Documentation Archive
0.12.0 Apr 17, 2019 Go to the documentation of bloc 0.12.0 Download bloc 0.12.0 archive
0.11.2 Apr 6, 2019 Go to the documentation of bloc 0.11.2 Download bloc 0.11.2 archive
0.11.1 Mar 30, 2019 Go to the documentation of bloc 0.11.1 Download bloc 0.11.1 archive
0.11.0 Mar 24, 2019 Go to the documentation of bloc 0.11.0 Download bloc 0.11.0 archive
0.10.0 Mar 19, 2019 Go to the documentation of bloc 0.10.0 Download bloc 0.10.0 archive
0.9.5 Mar 8, 2019 Go to the documentation of bloc 0.9.5 Download bloc 0.9.5 archive
0.9.4 Mar 8, 2019 Go to the documentation of bloc 0.9.4 Download bloc 0.9.4 archive
0.9.3 Feb 20, 2019 Go to the documentation of bloc 0.9.3 Download bloc 0.9.3 archive
0.9.2 Feb 16, 2019 Go to the documentation of bloc 0.9.2 Download bloc 0.9.2 archive
0.9.1 Feb 16, 2019 Go to the documentation of bloc 0.9.1 Download bloc 0.9.1 archive

All 42 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
98
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
99
Learn more about scoring.

We analyzed this package on Apr 4, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:bloc/bloc.dart.

Health suggestions

Fix lib/src/bloc_supervisor.dart. (-0.50 points)

Analysis of lib/src/bloc_supervisor.dart reported 1 hint:

line 14 col 30: Use ; instead of {} for empty constructor bodies.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
meta ^1.1.6 1.1.7
rxdart >=0.18.1 <0.21.0 0.20.0 0.21.0
Dev dependencies
mockito ^4.0.0
test >=1.3.0 <2.0.0
test_coverage ^0.2.0