Reduced 為流行的 Flutter 狀態管理框架中的核心狀態管理功能提供了一個統一的外觀。它透過提供一致的 API 來簡化整合,無論底層框架為何,都能輕鬆管理狀態、處理相依性並回應變更。專為希望在不產生樣板程式碼的情況下獲得彈性之開發者設計,Reduced 抽象了複雜性,同時維持效能與可擴展性。非常適合可能在多種狀態管理解決方案之間切換或組合使用的專案。
適用情境
簡化跨多個框架的狀態管理
建立可移植的狀態邏輯
減少有狀態小部件中的樣板程式碼
啟用框架無關的狀態處理
支援混合狀態管理方法
主要特色
統一的狀態管理 API
框架無關設計
最小化樣板程式碼
內建依賴注入支援
與現有狀態解決方案的無縫整合
適合對象
使用多個狀態管理庫的開發者
需要靈活狀態架構的專案
致力於降低複雜性的團隊
需要未來可擴展狀態設計的應用程式
使用可交換狀態模式進行原型設計
使用建議
由於抽象層可能導致輕微開銷
最適合中大型應用程式
需要理解底層狀態概念
僅限支援的狀態管理後端
不建議用於簡單的單次應用程式
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
reduced
Unified facade for the basic features of state management frameworks:
Read a current state value.
Update a current state value.
Callbacks with value semantics
The app logic based mainly on these features should remain as independent as possible from the state management framework used. Therefore, the app logic needs a neutral state management API and an implementation of this API for the state management framework used.
The former is provided by this package. The latter is provided by other packages listed at the end of the README.
Features
The 'reduced' facade covers the following features of a state management framework:
Instead of writing the state value directly, the facade provides a process method that accepts a called event as a parameter.
In the process method the event is executed with the current state value as a parameter and the return value of the event is stored as the new state value.
typedefSnapshotToPropsMapper<S, P> = P Function(
StoreSnapshot<S> snapshot,
String? routeName,
);
A SnapshotToPropsMapper is a Function that uses the snapshot and the routeName parameters to map the current state of a store into a derived 'selective' state value. Only the changes to this derived 'selective' state value determine whether a rebuild of the widget is triggered. In order for changes to be detected correctly, the derived 'selective' state value must have value semantics.
With a SnapshotToPropsMapper function usually a props parameter for a WidgetFromPropsBuilder function is created.
typedefWidgetFromPropsBuilder<P> = WidgetFunction({
Key? key,
required P props,
});
A WidgetFromPropsBuilder is a Function that builds a new widget from the properties of the passed props parameter. That is, the props parameter must contain all the properties necessary for building the widget.
Many widgets have callback properties of type Function. The Function type has no value semantics (each function is unique), this also applies to anonymous functions, which are often used as values for widget callback properties.
If value semantics are needed, Callable can be used for callback properties in the derived 'selective' state values instead of anonymous functions.
abstractclassCallable<T> {
T call();
}
abstractclassCallable1<T, V> {
T call(V value);
}
abstractclassCallable2<T, V1, V2> {
T call(V1 value1, V2 value2);
}
If the signatures of a widget callback property's function and the call method of a callable match, then the callable is assignment-compatible with the widget callback property, e.g.:
In the pubspec.yaml add dependencies on the package 'reduced' and on the package of an implementation of the 'reduced' facade for a state management framework, e.g. 'reduced_bloc'.
In addition to the basic features, state management frameworks also offer these advanced features:
Register a state for management.
Trigger a rebuild on widgets selectively after a state change.
Set up a new scope for state management.
A neutral facade has also been developed for this features in the form of the registerState function and the ReducedProvider, ReducedConsumer and ReducedScope widget classes. Since the features differ for the concrete frameworks, the signatures of the function and the widget constructors are also different, so these function and classes were not included in the 'reduced' facade, but are part of the additional API of the implementations of the 'reduced' facade.
1. Register a state for management.
voidregisterState(S initialValue);
classReducedProvider<S> extendsStatelesswidget{
ReducedProvider({
requiredthis.initialState,
requiredthis.child,
}) ...
final S initialState,
finalWidget child,
...
}
2. Trigger a rebuild on widgets selectively after a state change.