dynamic_middleware 0.5.1

Dynamic Middleware for Redux #

Build Status

Dart library that allows adding and removing Redux middleware after a Redux Store has already been initialized.

Warning: Some might say that doing this breaks the contract of idempotency you typically have with a Redux store. They're right! There are, however, some instances where this is useful. This library was created to support a new type of Redux store that is not idempotent.

Usage #

When creating your Redux Store simple attach a DynamicMiddleware object to it.

dynamicMiddleware = new DynamicMiddleware<State>.empty();
store = new Store<State>(reducer, middleware: [

Then add and remove middleware as you need.

dynamicMiddleware.add((store, action, next) => 
    print("Hello World!")

Use the DynamicMiddleware.fromSingle constructor to create new dynamic middleware with a single instance of Middleware.

store = new Store<State>(reducer, middleware: [
    new DynamicMiddleware.fromSingle(middleware1),

Use the DynamicMiddleware.fromList constructor to create new dynamic middleware from a List of Middleware.

waresList = <Middleware<State>>[middleware1, middleware2];
store = new Store<State>(reducer, middleware: [
    new DynamicMiddleware.fromList(waresList),

Adding and Removing Middleware #

Add a single piece of middleware.

void add(Middleware<State> middleware);

Add a List of middleware.

void addAll(List<Middleware<State>> middleware) => middlewares.addAll(middleware);

Remove a single piece of middleware.

void remove(Middleware<State> middleware) => middlewares.remove(middleware);

Remove all middleware previously added.

void clear();

Credits #


Changelog #

0.5.0 #

  • Update README.md

0.5.0 #

  • Adjust analysis_options.yaml rules.
  • Add empty and fromSingle constructors.
  • Follow the NextDispatcher pattern used by regular middleware.
  • Adjust tests and example to match updates.

0.4.0 #

  • Add toList() constructor and test it

0.3.0 #

  • Fix README.md

0.2.0 #

  • Add tests and example, Updated README.md

0.1.0 #

  • Initial version includes class DynamicMiddleware<State>


import 'dart:io';

import 'package:dynamic_middleware/dynamic_middleware.dart';
import 'package:redux/redux.dart';

/// Simple reducer that increments the state
int reducer(int state, Object action) => state+1;

/// Store that holds state value as an integer
Store<int> store;

/// Basic dynamic middleware object used to add and remove middleware
DynamicMiddleware<int> dynamicMiddleware;

/// Simple middleware that adds a line of standard output on all actions
void middleware(Store<int> store, Object action, NextDispatcher next) {
   stdout.writeln("Hello from dynamic middleware!");

void main() {
  dynamicMiddleware = new DynamicMiddleware<int>.empty();
  store = new Store<int>(reducer, initialState: 0, middleware: [dynamicMiddleware]);

  String input;
  while(input != "q") {
    stdout.writeln('Hit enter to dispatch an action,\nType "m" to dispatch an action with dynamic middleware,\nType "q" to quit');
    input = stdin.readLineSync();
    if(input == "m") {
        ..write("Counter: ")


Use this package as a library

1. Depend on it

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

  dynamic_middleware: ^0.5.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

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

3. Import it

Now in your Dart code, you can use:

import 'package:dynamic_middleware/dynamic_middleware.dart';
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.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://dart.dev/dart-2#migration.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.24.0 <2.0.0