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.