名称: asyncredux-setup 描述: 在 Flutter 应用中初始化、设置和配置 AsyncRedux。适用于启动新 AsyncRedux 项目或用户请求时。
AsyncRedux 设置
添加依赖
将 AsyncRedux 添加到 pubspec.yaml 中:
dependencies:
async_redux: ^25.6.1
检查 pub.dev 获取最新版本。
创建状态类
创建一个不可变的 AppState 类(在文件 app_state.dart 中),包含:
copy()方法==相等方法hashCode方法initialState()静态工厂方法
如果应用是新的,还没有任何状态,创建一个空的 AppState:
@immutable
class AppState {
AppState();
static AppState initialState() => AppState();
AppState copy() => AppState();
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is AppState && runtimeType == other.runtimeType;
@override
int get hashCode => 0;
}
如果存在现有状态,创建包含该状态的 AppState。
这是一个示例:
@immutable
class AppState {
final String name;
final int age;
AppState({required this.name, required this.age});
static AppState initialState() => AppState(name: "", age: 0);
AppState copy({String? name, int? age}) => AppState(
name: name ?? this.name,
age: age ?? this.age,
);
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is AppState &&
runtimeType == other.runtimeType &&
name == other.name &&
age == other.age;
@override
int get hashCode => Object.hash(name, age);
}
所有字段必须是 final(不可变)。根据需要添加其他辅助方法:
AppState withName(String name) => copy(name: name);
AppState withAge(int age) => copy(age: age);
创建存储
找到初始化应用的地方(通常在 main.dart 中),导入 AppState 类(根据需要调整路径)和 AsyncRedux 包:
import 'app_state.dart';
import 'package:async_redux/async_redux.dart';
使用初始状态创建存储。注意 PersistorDummy、GlobalWrapErrorDummy 和 ConsoleActionObserver 由 AsyncRedux 提供用于基本设置。将来可以根据需要替换为自定义实现。
late Store store;
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 创建持久化器,并尝试读取之前保存的状态。
var persistor = PersistorDummy<AppState>();
AppState? initialState = await persistor.readState();
// 如果没有保存的状态,创建一个新的空状态并保存它。
if (initialState == null) {
initialState = AppState.initialState();
await persistor.saveInitialState(initialState);
}
// 创建存储。
store = Store<AppState>(
initialState: initialState,
persistor: persistor,
globalWrapError: GlobalWrapErrorDummy(),
actionObservers: [ConsoleActionObserver()],
);
runApp(...);
}
使用 StoreProvider 包装
使用 StoreProvider 包装应用以使存储可访问。
找到应用的小部件树的根,并在 MaterialApp(或 CupertinoApp,根据需要调整)上方添加它。注意您需要导入 store。
import 'package:async_redux/async_redux.dart';
Widget build(context) {
return StoreProvider<AppState>(
store: store,
child: MaterialApp( ... ),
);
}
必需的上文扩展
您必须将此扩展添加到包含 AppState 的文件中(这是为了在小部件中更容易访问状态所必需的):
extension BuildContextExtension on BuildContext {
AppState get state => getState<AppState>();
AppState read() => getRead<AppState>();
R select<R>(R Function(AppState state) selector) =>
getSelect<AppState, R>(selector);
R? event<R>(Evt<R> Function(AppState state) selector) =>
getEvent<AppState, R>(selector);
}
必需的基础操作
创建文件 app_action.dart,包含此抽象类扩展 ReduxAction<AppState>:
/// 所有操作扩展此类。
abstract class AppAction extends ReduxAction<AppState> {
ActionSelect get select => ActionSelect(state);
}
// 专用选择器类以保持基础操作简洁。
class ActionSelect {
final AppState state;
ActionSelect(this.state);
}
更新 CLAUDE.md
将以下信息添加到项目的 CLAUDE.md 中,以便所有操作扩展此基础操作:
## 基础操作
所有操作应扩展 `AppAction` 而不是 `ReduxAction<AppState>`。
有一个专用选择器类叫 `ActionSelect`,通过将选择器命名空间化在 `select` 下并启用 IDE 自动完成来保持基础操作简洁。示例:
```dart
class ProcessItem extends AppAction {
final String itemId;
ProcessItem(this.itemId);
@override
AppState reduce() {
// IDE 自动完成显示:select.findById, select.completed 等。
final item = select.findById(itemId);
// ...
}
}