搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案