搜索插件、代码仓与 APP 分类

插件详情v0.4.0

redux_thunk

一个 Redux.dart 中间件,允许您分派执行异步工作的函数作为操作。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2.7万
喜欢数
66
Pub 分数
160

安装插件

flutter pub add redux_thunk

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

redux_thunk 是 Redux.dart 的中间件,允许将函数(thunks)作为操作进行分发,从而在 Redux 状态管理流程中处理异步操作,如 API 调用或延迟任务。它通过在动作创建器中直接支持异步逻辑,扩展了 Redux 的动作分发机制,提高了代码的组织性和可维护性。该包与使用 Redux.dart 的 Flutter 应用无缝集成,使涉及副作用的复杂状态转换更易于管理。在需要数据获取、身份验证流程或后台处理的应用中尤其有用。

适合场景

  • 处理异步 API 调用
  • 管理身份验证工作流
  • 执行延迟或条件操作
  • 简化复杂的状态更新
  • 在 Redux 中实现副作用
  • 协调多个异步操作

主要特点

  • 支持异步操作创建函数
  • 与 Redux.dart 集成
  • 启用函数分发
  • 干净地管理副作用
  • 减少异步逻辑的样板代码
  • 适用于所有 Flutter 平台

适合对象

  • 使用 Redux.dart 的 Flutter 应用
  • 开发人员管理异步状态逻辑
  • 需要结构化副作用处理的团队
  • 频繁进行 API 交互的应用程序
  • 需要可预测状态变更的项目
  • 复杂的状态管理场景

使用建议

  • 需要 Redux.dart 设置
  • 为简单应用增加复杂性
  • 可能需要额外的错误处理
  • 如果使用更简单的状态解决方案,则不需要
  • 最适合大规模应用
  • 补充而非替代其他状态管理工具

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目