redux_future_middleware 0.1.0+1 redux_future_middleware: ^0.1.0+1 copied to clipboard
A Middleware for Redux which makes handling Dart Future easy by using Actions.
Redux Future Middleware (Dart/Flutter) #
Redux middleware package for handling Dart Futures by creating a FutureAction.
The futureMiddleware
can be attached to the Redux
store upon construction.
Once attached, you can store.dipatch
the FutureAction
, and the futureMiddleware
will intercept it.
- If the
Future
passed inFutureAction
completes successfully, aFutureSuccessAction
will be dipatched with the result of theFuture
. - If the
Future
passed inFutureAction
fails, aFutureErrorAction
will be dispatched containing the error that was returned. - When the
FutureAction
is dipatches, aFuturePendingAction
is dispatched from thefutureMiddleware
for consumption by theReducer
.
Usage: #
main() {
// First, create an action to uniquely identify each FutureAction from others.
class ExampleAction {}
// Then, create a reducer that knows how to handle the future actions
String exampleReducer(String state, dynamic action) {
if (action is FuturePendingAction<ExampleAction>) {
return 'Fetching';
} else if (action is FutureSuccessAction<ExampleAction, String>) {
return action.payload;
} else if (action is FutureErrorAction<ExampleAction>) {
return action.error.toString();
}
return state;
}
// Next, create a store that includes `futureMiddleware`. It will
// intercept all `FutureAction` that are dispatched.
final store = Store(
exampleReducer,
middleware: [futureMiddleware],
);
// Next, dispatch `FutureAction` for intercepting.
store.dipatch(FutureAction<ExampleAction, String>(
future: Future.value('Hi')));
}
Credits: #
- Brian Egan, for the inspiration on developing it.
- Ibrahim Mubarak Hussain, for lending a helping hand.