Search packages, repositories and app categories

Package detailsv0.4.0

redux_thunk

A Redux.dart Middleware that allows you to dispatch functions that perform async work as actions.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
27K
Likes
66
Pub points
160

Install package

flutter pub add redux_thunk

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

redux_thunk is a middleware for Redux.dart that enables dispatching functions (thunks) as actions, allowing asynchronous operations like API calls or delayed tasks to be handled within the Redux state management flow. It extends Redux's action dispatch mechanism by supporting async logic directly in action creators, improving code organization and maintainability. This package integrates seamlessly with Flutter apps using Redux.dart, making it easier to manage complex state transitions involving side effects. It is particularly useful in apps requiring data fetching, authentication flows, or background processing.

Use cases

  • Handling async API calls
  • Managing authentication workflows
  • Executing delayed or conditional actions
  • Simplifying complex state updates
  • Implementing side effects in Redux
  • Coordinating multiple async operations

Key features

  • Supports async action creators
  • Integrates with Redux.dart
  • Enables dispatching functions
  • Manages side effects cleanly
  • Reduces boilerplate for async logic
  • Works across all Flutter platforms

Suitable for

  • Flutter apps using Redux.dart
  • Developers managing async state logic
  • Teams needing structured side effect handling
  • Apps with frequent API interactions
  • Projects requiring predictable state changes
  • Complex state management scenarios

Considerations

  • Requires Redux.dart setup
  • Adds complexity for simple apps
  • May need additional error handling
  • Not needed if using simpler state solutions
  • Best suited for large-scale applications
  • Complements rather than replaces other state management tools

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

redux_thunk

Build Status codecov

Redux provides a simple way to update a your application's State in response to synchronous Actions. However, it lacks tools to handle asynchronous code. This is where Thunks come in.

The thunkMiddleware intercepts and calls ThunkActions, which is simply a fancy name for any function that takes 1 argument: a Redux Store. This allows you to dispatch functions (aka ThunkActions) to your Store that can perform asynchronous work, then dispatch actions using the Store after the work is complete.

The dispatched ThunkActions will be swallowed, meaning they will not go through the rest of your middleware to the Store's Reducer.

Example
// First, create a quick reducer
State reducer(String state, action) =>
    action is String ? action : state;

// Next, apply the `thunkMiddleware` to the Store
final store = new Store<String>(
  reducer,
  middleware: [thunkMiddleware],
);

// Create a `ThunkAction`, which is any function that accepts the 
// Store as it's only argument. Our function (aka ThunkAction) will
// simply send an action after 1 second.  This is just an example, 
// but  in real life, you could make a call to an HTTP service or 
// database instead!
void action(Store<String> store) async {
  final searchResults = await new Future.delayed(
    new Duration(seconds: 1),
    () => "Search Results",
  );

  store.dispatch(searchResults);
}

// You can also use a function with some parameters to create a thunk, 
// and then use it like so: store.dispatch(waitAndDispatch(3));
ThunkAction<String> waitAndDispatch(int secondsToWait) {
  return (Store<String> store) async {
    final searchResults = await new Future.delayed(
      new Duration(seconds: secondsToWait),
      () => "Search Results",
    );

    store.dispatch(searchResults);
  };
}


Credits

All the ideas in this lib are shamelessly stolen from the original redux-thunk library and simply adapted to Dart.

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package