bloc_redux 0.8.0

  • Example
  • Installing
  • Versions
  • new55


Bloc_Redux is a combination of Bloc and Redux. OK, get it, but why?

Redux is an elegant pattern, which simplify the state management, and makes an unidirectional data flow. but it needs reducers return another state. by default it will make any widget using this state rebuild no matter if it has been modified. optimization can be made like diff to see the real changed properties, but not easy to do in flutter without reflection.

BLoC is a more general idea, UI trigger events using bloc's sink method, bloc handle it then change stream, UI receive latest stream value to reflect the change. it's vague on how to combine multi blocs or should there be only one per page? so it's more an idea than a spec.

so to get the most of both, here comes Bloc_Redux.


  • actions are the only way to change state.
  • blocs handle actions just like reducers but don't produce new states.
  • widgets' data comes from state's stream, binded.

User trigger a button, it produces an action send to blocs, blocs which are interested in this action do some bussiness logic then change state by adding something new to stream, since streams are bind to widgets, UI are automatically updated.



/// Used by StateInput and Store.
/// When `StoreProvider` is disposed, it will call `BRStore`.dispose
/// which will call StateInput.dispose to close stream.
abstract class Disposable {
  void dispose();

/// Useful when combined with StreamBuilder
class StreamWithInitialData<T> {
  final Stream<T> stream;
  final T initialData;

  StreamWithInitialData(, this.initialData);

/// Action
/// every action should extends this class
abstract class BRAction<T> {
  T playload;

/// State
/// Input are used to change state.
/// usually filled with StreamController / BehaviorSubject.
/// handled by blocs.
/// implements disposable because stream controllers needs to be disposed.
/// they will be called within store's dispose method.
abstract class BRStateInput implements Disposable {}

/// Output are streams.
/// followed by input. like
/// UI will use it as data source.
abstract class BRStateOutput {}

/// State
/// Combine these two into one.
abstract class BRState<T extends BRStateInput, U extends BRStateOutput> {
  T input;
  U output;

/// Bloc
/// like reducers in redux, but don't return a new state.
/// when they found something needs to change, just update state's input
/// then state's output will change accordingly.
typedef Bloc<T extends BRStateInput> = void Function(BRAction action, T input);

/// Store
abstract class BRStore<T extends BRStateInput, U extends BRState>
    implements Disposable {
  List<Bloc<T>> blocs;
  U state;

  void dispatch(BRAction action) {
    blocs.forEach((f) => f(action, state.input));

  // store will be disposed when provider disposed.
  dispose() {

StreamWithInitialData is useful when consumed by StreamBuilder which needs initialData.


borrowed from here

Type _typeOf<T>() => T;

class StoreProvider<T extends BRStore> extends StatefulWidget {
    Key key,
    @required this.child,
  }) : super(key: key);

  final Widget child;
  final T store;

  _StoreProviderState<T> createState() => _StoreProviderState<T>();

  static T of<T extends BRStore>(BuildContext context) {
    final type = _typeOf<_StoreProviderInherited<T>>();
    _StoreProviderInherited<T> provider =
    return provider?.store;

class _StoreProviderState<T extends BRStore> extends State<StoreProvider<T>> {
  void dispose() {;

  Widget build(BuildContext context) {
    return new _StoreProviderInherited<T>(
      child: widget.child,

class _StoreProviderInherited<T> extends InheritedWidget {
    Key key,
    @required Widget child,
  }) : super(key: key, child: child);

  final T store;

  bool updateShouldNotify(_StoreProviderInherited oldWidget) => false;


Color Demo

/// Actions
class ColorActionSelect extends BRAction<Color> {}

/// State
class ColorStateInput extends BRStateInput {
  final BehaviorSubject<Color> selectedColor = BehaviorSubject();
  final BehaviorSubject<List<ColorModel>> colors = BehaviorSubject();

  dispose() {

class ColorStateOutput extends BRStateOutput {
  StreamWithInitialData<Color> selectedColor;
  StreamWithInitialData<List<ColorModel>> colors;

  ColorStateOutput(ColorStateInput input) {
    selectedColor = StreamWithInitialData(, input.selectedColor.value);
    colors = StreamWithInitialData(, input.colors.value);

class ColorState extends BRState<ColorStateInput, ColorStateOutput> {
  ColorState() {
    input = ColorStateInput();

    var _colors = List<ColorModel>.generate(
        30, (int index) => ColorModel(RandomColor(index).randomColor()));
    _colors[0].isSelected = true;

    output = ColorStateOutput(input);

/// Blocs
Bloc<ColorStateInput> colorSelectHandler = (action, input) {
  if (action is ColorActionSelect) {
    var colors = input.colors.value
        .map((colorModel) => colorModel
          ..isSelected = colorModel.color.value == action.playload.value)

/// Store
class ColorStore extends BRStore<ColorStateInput, ColorState> {
  ColorStore() {
    state = ColorState();
    blocs = [colorSelectHandler];

State is separated into input and output, input is used by blocs, if bloc find it's necessary to change state, it can add something new to stream. widgets will receive this change immediately by listening output.



  • fix typo (action.playload -> action.payload)


  • remove disposable.dart, inject into bloc_redux.dart


  • update package description


  • add an example file for dart pub




// see for more demos

Use this package as a library

1. Depend on it

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

  bloc_redux: ^0.8.0

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter packages get

Alternatively, your editor might support 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_redux/bloc_redux.dart';
Version Uploaded Documentation Archive
0.8.0 Jan 5, 2019 Go to the documentation of bloc_redux 0.8.0 Download bloc_redux 0.8.0 archive
0.7.0 Jan 5, 2019 Go to the documentation of bloc_redux 0.7.0 Download bloc_redux 0.7.0 archive
0.6.2 Jan 4, 2019 Go to the documentation of bloc_redux 0.6.2 Download bloc_redux 0.6.2 archive
0.6.1 Jan 4, 2019 Go to the documentation of bloc_redux 0.6.1 Download bloc_redux 0.6.1 archive
0.6.0 Jan 4, 2019 Go to the documentation of bloc_redux 0.6.0 Download bloc_redux 0.6.0 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

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

  • Dart: 2.1.0
  • pana: 0.12.10
  • Flutter: 1.1.7


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Fix lib/bloc_redux.dart. (-0.50 points)

Analysis of lib/bloc_redux.dart reported 1 hint:

line 11 col 9: The class 'Stream' was not exported from 'dart:core' until version 2.1, but this code is required to be able to run on earlier versions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8