搜尋套件、儲存庫與 APP 分類

套件詳情v5.0.0

redux

Redux 是用於 Dart 和 Flutter 應用的可預測狀態容器

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
8.3萬
喜歡數
417
Pub 分數
135

安裝套件

flutter pub add redux

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Redux 是一個為 Dart 與 Flutter 應用程式設計的可預測狀態管理解決方案,靈感來自 JavaScript 中的 Redux。它提供了一個中央儲存區來管理應用程式狀態,透過純還原函數確保一致的更新。支援中間件與開發工具整合,能實現除錯與時間旅行功能。專為可擴展性設計,非常適合需要在多個元件之間進行結構化狀態處理的中大型應用程式。

適用情境

  • 管理複雜的應用程式狀態
  • 啟用時間旅行除錯
  • 支援大規模 Flutter 應用
  • 與中介軟體整合以處理副作用
  • 促進團隊在狀態邏輯上的協作

主要特色

  • 集中式狀態儲存
  • 不可變狀態更新
  • 中間件支援
  • DevTools 結合
  • 可預測的狀態轉換

適合對象

  • 大型 Flutter 項目
  • 需要結構化狀態管理的團隊
  • 需要除錯功能的應用程式
  • 熟悉 Redux 模式的開發人員
  • 具有複雜數據流的項目

使用建議

  • 初學者的學習曲線更陡峭
  • 所需的樣板程式碼
  • 對於簡單應用來說過度設計
  • 需要仔細設計狀態
  • 有限的內建非同步處理

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Build Status codecov Flutter Community: redux

Redux for Dart using generics for typed State. It includes a rich ecosystem of Docs, Middleware, Dev Tools and can be combined with Flutter using the flutter_redux package.

Docs

  • Motivation and Principles - Learn why Redux might make sense for your app and the principles behind it.
  • Basics - Introduction to the core concepts in Redux
  • Combining Reducers - combineReducers works a bit differently in Dart than it does in JS. Learn why, and how to combine reducers in a type-safe way!
  • Async with Middleware - Learn how to make async calls, such as to a web service or local database, with Redux.
  • Use Selectors to Query Data in the Store - Learn how to write functions to provide consistent access to data in your App State.
  • API Documentation - Rich documentation included in the source code and generated by DartDoc.

Flutter Examples

To integrate Redux.dart with Flutter, please use the flutter_redux package.

Beginner
Intermediate
Advanced

Middleware

  • redux_logging - Connects a Logger to a Store, and can print out actions as they're dispatched to your console.
  • redux_thunk - Allows you to dispatch functions that perform async work as actions.
  • redux_future - For handling Dart Futures that are dispatched as Actions.
  • redux_epics - Middleware that allows you to work with Dart Streams of Actions to perform async work.

Dev Tools

The redux_dev_tools library allows you to create a DevToolsStore during dev mode in place of a normal Redux Store.

This DevToolsStore will act exactly like a normal Store at first. However, it will also allow you to travel back and forth throughout the States of your app or recompute the State of your app by replaying all actions through your reducers. This works perfectly with Hot Reloading!

You can combine the DevToolsStore with your own UI to travel in time, or use one of the existing options for the platform you're working with:

Additional Utilities

  • reselect - Efficiently derive data from your Redux Store with memoized functions.
  • redux_persist - Persist Redux State, works for Web and Flutter

Usage

import 'package:redux/redux.dart';

// Create typed actions. You will dispatch these in order to
// update the state of your application.
enum Actions {
  increment,
  decrement,
}

// Create a Reducer. A reducer is a pure function that takes the 
// current State (int) and the Action that was dispatched. It should
// combine the two into a new state without mutating the state passed
// in! After the state is updated, the store will emit the update to 
// the `onChange` stream.
// 
// Because reducers are pure functions, they should not perform any 
// side-effects, such as making an HTTP request or logging messages
// to a console. For that, use Middleware.
int counterReducer(int state, action) {
  if (action == Actions.increment) {
    return state + 1;
  } else if (action == Actions.decrement) {
    return state - 1;
  }
  
  return state;
}

// A piece of middleware that will log all actions with a timestamp
// to your console!
// 
// Note, this is just an example of how to write your own Middleware.
// See the redux_logging package on pub for a pre-built logging 
// middleware.
loggingMiddleware(Store<int> store, action, NextDispatcher next) {
  print('${new DateTime.now()}: $action');

  next(action);
}

main() {
  // Create the store with our Reducer and Middleware
  final store = new Store<int>(
    counterReducer, 
    initialState: 0, 
    middleware: [loggingMiddleware],
  );

  // Render our State right away
  render(store.state);
  
  // Listen to store changes, and re-render when the state is updated
  store.onChange.listen(render);

  // Attach a click handler to a button. When clicked, the `INCREMENT` action
  // will be dispatched. It will then run through the reducer, updating the 
  // state.
  //
  // After the state changes, the html will be re-rendered by our `onChange`
  // listener above. 
  querySelector('#increment').onClick.listen((_) {
    store.dispatch(Actions.increment);
  });
}

render(int state) {
  querySelector('#value').innerHtml = '${state}';
}

所屬分類

原始碼儲存庫

使用此套件的專案

所在榜單