Viewing version 0.11.2. Open the latest package page.
bloc
0.11.2A predictable state management library that helps implement the BLoC (Business Logic Component) design pattern.
Readme
Pub Build Status codecov Flutter.io Awesome Flutter Flutter Samples 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
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 the incoming event as an argument. mapEventToState is called whenever an event is dispatched by the presentation layer. mapEventToState must convert that event 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(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) {
super.onTransition(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) {
super.onTransition(transition);
print(transition);
}
@override
void onError(Object error, StackTrace stacktrace) {
super.onError(error, 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
CounterBlocin a pure Dart app.
Maintainers
Changelog
0.11.2
Added BlocDelegate onError and onTransition mustCallSuper and Documentation Updates
0.11.1
Added dispose mustCallSuper and Documentation Updates
0.11.0
Update mapEventToState to remove unnecessary argument for currentState
Stream<S> mapEventToState(S currentState, E event)->Stream<S> mapEventToState(E event)- Documentation Updates
- Example Updates
0.10.0
Updated to rxdart ^0.21.0 and Documentation Updates
0.9.5
Minor Enhancements to Code Style and Documentation.
0.9.4
Calls to dispatch 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
onErrortoBlocfor local error handling. - Added
onErrortoBlocDelegatefor 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
BlocwithmapEventToStatewhich returns multiple states per event will now correctly report theTransitions.
0.7.1
Improvements to Bloc usage in pure Dart applications.
Blocstate is seeded withinitialStateautomatically
0.7.0
Added BlocSupervisor and BlocDelegate.
BlocSupervisornotifiesBlocDelegateofTransitionsBlocDelegateexposesonTransitionwhich is invoked for allBlocTransitions.
0.6.0
Transitions and initialState updates.
- Added
Transitions andonTransition - Made
initialStaterequired
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
Blocclass. - Includes the ability to connect presentation layer to
Blocby using theBlocBuilderWidget.
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'bloc:0.11.2'pubspec.yaml
name: bloc
description: A predictable state management library that helps implement the BLoC (Business Logic Component) design pattern.
version: 0.11.2
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.21.0
dev_dependencies:
test: ">=1.3.0 <2.0.0"
test_coverage: ^0.2.0
mockito: ^4.0.0
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.1.0 | Cached · Upstream SHA-256 matched | Download | bfe4b4a2…724c57 |
| 0.1.1 | Cached · Upstream SHA-256 matched | Download | 86193bc3…e3b0fa |
| 0.1.2 | Cached · Upstream SHA-256 matched | Download | 5f3eaad8…a2abad |
| 0.2.0 | Cached · Upstream SHA-256 matched | Download | e16fd926…2ed9c4 |
| 0.2.1 | Cached · Upstream SHA-256 matched | Download | b56bf959…942cc5 |
| 0.2.2 | Cached · Upstream SHA-256 matched | Download | fcc7a7cc…6b2af7 |
| 0.2.3 | Cached · Upstream SHA-256 matched | Download | 9e35b326…dafd48 |
| 0.2.4 | Cached · Upstream SHA-256 matched | Download | 1231c70c…4c85b3 |
| 0.2.5 | Cached · Upstream SHA-256 matched | Download | b805a9e0…1a0977 |
| 0.3.0 | Cached · Upstream SHA-256 matched | Download | 3699626f…482ae3 |
| 0.4.0 | Cached · Upstream SHA-256 matched | Download | 77d3150d…6bddd5 |
| 0.4.1 | Cached · Upstream SHA-256 matched | Download | 83ca5fac…7a91ce |
| 0.4.2 | Cached · Upstream SHA-256 matched | Download | 53dc13e2…9fc2ef |
| 0.5.0 | Cached · Upstream SHA-256 matched | Download | 3fbb3f16…d502ca |
| 0.5.1 | Cached · Upstream SHA-256 matched | Download | 2a89d793…b470fb |
| 0.5.2 | Cached · Upstream SHA-256 matched | Download | 552344cd…1d6ab4 |
| 0.6.0 | Cached · Upstream SHA-256 matched | Download | 45eddbfa…fe8874 |
| 0.7.0 | Cached · Upstream SHA-256 matched | Download | 41e0f5bc…d7cba4 |
| 0.7.1 | Cached · Upstream SHA-256 matched | Download | b5acda46…c78e71 |
| 0.7.2 | Cached · Upstream SHA-256 matched | Download | b9b0dfbb…4785f4 |
| 0.7.3 | Cached · Upstream SHA-256 matched | Download | c159b538…b53225 |
| 0.7.4 | Cached · Upstream SHA-256 matched | Download | 1ebcf265…c50c26 |
| 0.7.5 | Cached · Upstream SHA-256 matched | Download | 1ab88a6d…f01790 |
| 0.7.6 | Cached · Upstream SHA-256 matched | Download | 2dc55af6…f57cf9 |
| 0.7.7 | Cached · Upstream SHA-256 matched | Download | 2ea3af00…f4a789 |
| 0.7.8 | Cached · Upstream SHA-256 matched | Download | 88673fa0…01d8af |
| 0.8.0 | Cached · Upstream SHA-256 matched | Download | 607e8450…bf202d |
| 0.8.1 | Cached · Upstream SHA-256 matched | Download | c495b55a…f41e07 |
| 0.8.2 | Cached · Upstream SHA-256 matched | Download | c2b3a781…2f7f8a |
| 0.8.3 | Cached · Upstream SHA-256 matched | Download | 7486fa77…a18588 |
| 0.8.4 | Cached · Upstream SHA-256 matched | Download | c653498b…57bfa1 |
| 0.9.0 | Cached · Upstream SHA-256 matched | Download | f5ddef39…b23d5d |
| 0.9.1 | Cached · Upstream SHA-256 matched | Download | 903bb05d…67ac8f |
| 0.9.2 | Cached · Upstream SHA-256 matched | Download | 3b35b9fb…a170fd |
| 0.9.3 | Cached · Upstream SHA-256 matched | Download | 092530d5…03b8b2 |
| 0.9.4 | Cached · Upstream SHA-256 matched | Download | 32ec9ec6…b81639 |
| 0.9.5 | Cached · Upstream SHA-256 matched | Download | c95dc63e…25cd90 |
| 0.10.0 | Cached · Upstream SHA-256 matched | Download | 35a83cde…96fdc4 |
| 0.11.0 | Cached · Upstream SHA-256 matched | Download | 0a0a388b…8a0e85 |
| 0.11.1 | Cached · Upstream SHA-256 matched | Download | 499e942f…89cce8 |
| 0.11.2 | Cached · Upstream SHA-256 matched | Download | dd9ea891…987395 |
| 0.12.0 | Cached · Upstream SHA-256 matched | Download | 870bb53d…09ef1b |
| 0.13.0 | Cached · Upstream SHA-256 matched | Download | cfcc09c3…19ba3c |
| 0.14.0 | Cached · Upstream SHA-256 matched | Download | fc65c2e2…e13393 |
| 0.14.1 | Cached · Upstream SHA-256 matched | Download | af2ea6e1…09681c |
| 0.14.2 | Cached · Upstream SHA-256 matched | Download | 8f89f2e1…03c797 |
| 0.14.3 | Cached · Upstream SHA-256 matched | Download | cb54ce33…1790c7 |
| 0.14.4 | Cached · Upstream SHA-256 matched | Download | a2971321…9af79c |
| 0.15.0 | Cached · Upstream SHA-256 matched | Download | 4dc615b1…e49e76 |
| 0.16.0 | Cached · Upstream SHA-256 matched | Download | 4a9df2c4…c63dfd |
| 0.16.1 | Cached · Upstream SHA-256 matched | Download | d87523ac…a6635f |
| 1.0.0 | Cached · Upstream SHA-256 matched | Download | 0d57df01…063f4a |
| 1.0.1 | Cached · Upstream SHA-256 matched | Download | b2592b70…f07cdc |
| 2.0.0 | Cached · Upstream SHA-256 matched | Download | 8544ef60…ec3828 |
| 3.0.0-dev.1 | Cached · Upstream SHA-256 matched | Download | 9c23c45e…f14457 |
| 3.0.0 | Cached · Upstream SHA-256 matched | Download | ccc49d51…61ada4 |
| 4.0.0-dev.1 | Cached · Upstream SHA-256 matched | Download | 597621d1…62e462 |
| 4.0.0-dev.2 | Cached · Upstream SHA-256 matched | Download | e21f7cfb…960bc8 |
| 4.0.0-dev.3 | Cached · Upstream SHA-256 matched | Download | 641593fe…d90033 |
| 4.0.0-dev.4 | Cached · Upstream SHA-256 matched | Download | 92820ef4…99d334 |
| 4.0.0 | Cached · Upstream SHA-256 matched | Download | bc0e8756…bac75a |
| 5.0.0-dev.1 | Cached · Upstream SHA-256 matched | Download | 488a1a1f…9d0d2e |
| 5.0.0-dev.2 | Cached · Upstream SHA-256 matched | Download | 2720922c…c03302 |
| 5.0.0-dev.3 | Cached · Upstream SHA-256 matched | Download | 8d2480cf…e4f067 |
| 5.0.0-dev.4 | Cached · Upstream SHA-256 matched | Download | 3edd01b5…db4dd9 |
| 5.0.0-dev.5 | Cached · Upstream SHA-256 matched | Download | 6639b001…7a8dac |
| 5.0.0-dev.6 | Cached · Upstream SHA-256 matched | Download | 5293933b…a20eb5 |
| 5.0.0-dev.7 | Cached · Upstream SHA-256 matched | Download | 21456eff…2a4b1e |
| 5.0.0-dev.8 | Cached · Upstream SHA-256 matched | Download | 8b8a74e0…0b8435 |
| 5.0.0-dev.9 | Cached · Upstream SHA-256 matched | Download | d0af89ca…0ebd1d |
| 5.0.0-dev.10 | Cached · Upstream SHA-256 matched | Download | d6513650…a4f00e |
| 5.0.0-dev.11 | Cached · Upstream SHA-256 matched | Download | 96f45d25…c4d29b |
| 5.0.0 | Cached · Upstream SHA-256 matched | Download | 66e5e429…5b9754 |
| 5.0.1 | Cached · Upstream SHA-256 matched | Download | 872e0786…f433d4 |
| 6.0.0-dev.1 | Cached · Upstream SHA-256 matched | Download | dde227e1…8a7beb |
| 6.0.0-dev.2 | Cached · Upstream SHA-256 matched | Download | 0a907ea1…b188e8 |
| 6.0.0 | Cached · Upstream SHA-256 matched | Download | 9bba4d00…9dddf5 |
| 6.0.1 | Cached · Upstream SHA-256 matched | Download | 28f8ae0e…c0e167 |
| 6.0.2 | Cached · Upstream SHA-256 matched | Download | da311fd0…9058c3 |
| 6.0.3 | Cached · Upstream SHA-256 matched | Download | e4a4e6b2…7ac1fa |
| 6.1.0 | Cached · Upstream SHA-256 matched | Download | 527c4923…233ea2 |
| 6.1.1 | Cached · Upstream SHA-256 matched | Download | d6e1fb79…6ab57e |
| 6.1.2 | Cached · Upstream SHA-256 matched | Download | 7108b980…aa60f4 |
| 6.1.3 | Cached · Upstream SHA-256 matched | Download | 428a6baa…bc826b |
| 7.0.0-nullsafety.0 | Cached · Upstream SHA-256 matched | Download | d9d8220e…554ea5 |
| 7.0.0-nullsafety.1 | Cached · Upstream SHA-256 matched | Download | 1c277595…a8344d |
| 7.0.0-nullsafety.2 | Cached · Upstream SHA-256 matched | Download | 2c6e21d5…0b9875 |
| 7.0.0-nullsafety.3 | Cached · Upstream SHA-256 matched | Download | 239c91fb…fc7d9c |
| 7.0.0-nullsafety.4 | Cached · Upstream SHA-256 matched | Download | 564071b0…4c0c64 |
| 7.0.0 | Cached · Upstream SHA-256 matched | Download | 8fb2e405…5f1026 |
| 7.1.0 | Cached · Upstream SHA-256 matched | Download | 2ce4a471…1a4a13 |
| 7.2.0-dev.1 | Cached · Upstream SHA-256 matched | Download | 3a583482…35aa1f |
| 7.2.0-dev.2 | Cached · Upstream SHA-256 matched | Download | f3e36406…3dc0e9 |
| 7.2.0-dev.3 | Cached · Upstream SHA-256 matched | Download | 9cd52452…65b6d4 |
| 7.2.0 | Cached · Upstream SHA-256 matched | Download | 250bf997…439333 |
| 7.2.1 | Cached · Upstream SHA-256 matched | Download | 6f1b87b6…a516bd |
| 8.0.0-dev.1 | Cached · Upstream SHA-256 matched | Download | 37f9b6cd…6a6866 |
| 8.0.0-dev.2 | Cached · Upstream SHA-256 matched | Download | d6e6c205…b0c7a9 |
| 8.0.0-dev.3 | Cached · Upstream SHA-256 matched | Download | b64573ee…e985bd |
| 8.0.0-dev.4 | Cached · Upstream SHA-256 matched | Download | c21e5efa…9f82e0 |
| 8.0.0-dev.5 | Cached · Upstream SHA-256 matched | Download | d489783d…cc0b16 |
| 8.0.0 | Cached · Upstream SHA-256 matched | Download | a9bd93ba…8baae3 |
| 8.0.1 | Cached · Upstream SHA-256 matched | Download | c12099a6…9dd7e0 |
| 8.0.2 | Cached · Upstream SHA-256 matched | Download | 104c7223…98f32d |
| 8.0.3 | Cached · Upstream SHA-256 matched | Download | 318e6cc6…b88e03 |
| 8.1.0 | Cached · Upstream SHA-256 matched | Download | bd4f8027…44534a |
| 8.1.1 | Cached · Upstream SHA-256 matched | Download | 658a5ae5…e11a80 |
| 8.1.2 | Cached · Upstream SHA-256 matched | Download | 3820f15f…be5b49 |
| 8.1.3 | Cached · Upstream SHA-256 matched | Download | f53a110e…a2db9e |
| 8.1.4 | Cached · Upstream SHA-256 matched | Download | 106842ad…77804e |
| 9.0.0 | Cached · Upstream SHA-256 matched | Download | 52c10575…6ac189 |
| 9.0.1 | Cached · Upstream SHA-256 matched | Download | e18b8e78…994d78 |
| 9.1.0 | Cached · Upstream SHA-256 matched | Download | a2cebb89…74537d |
| 9.2.0 | Cached · Upstream SHA-256 matched | Download | a48653a8…7efb81 |
| 9.2.1 | Cached · Upstream SHA-256 matched | Download | e03b2359…b75413 |
Dependencies
meta "^1.1.6"
rxdart "^0.21.0"