一键导入
asyncredux-sync-actions
Creates AsyncRedux (Flutter) synchronous actions that update state immediately by implementing reduce() to return a new state.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Creates AsyncRedux (Flutter) synchronous actions that update state immediately by implementing reduce() to return a new state.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Inject dependencies into actions using the environment, dependencies, and configuration pattern. Covers creating an Environment enum, a Dependencies class, passing them to the Store, accessing them from actions and widgets, and using dependency injection for testability.
Create a custom base action class for your app. Covers adding getter shortcuts to state parts, adding selector methods, implementing shared wrapError logic, and establishing project-wide action conventions.
Initialize, setup and configure AsyncRedux in a Flutter app. Use it whenever starting a new AsyncRedux project, or when the user requests.
Stops an AsyncRedux (Flutter) action from dispatching. Use only when the user mentions abortDispatch(), or explicitly asks to abort or prevent dispatch under certain conditions.
Checks an AsyncRedux (Flutter) action's completion status using ActionStatus right after the dispatch returns. Use only when you need to know whether an action completed, whether it failed with an error, what error it produced, or how to navigate based on success or failure.
Creates AsyncRedux (Flutter) actions that return null from reduce() to not change the state. Such actions can still do side effects, dispatch other actions, or do nothing.
| name | asyncredux-sync-actions |
| description | Creates AsyncRedux (Flutter) synchronous actions that update state immediately by implementing reduce() to return a new state. |
A synchronous action returns AppState? from its reduce() method. The action completes
immediately and state updates right away.
class Increment extends ReduxAction<AppState> {
@override
AppState? reduce() => state.copy(counter: state.counter + 1);
}
Every action extends ReduxAction<AppState>:
class MyAction extends ReduxAction<AppState> {
@override
AppState? reduce() {
// Return new state
}
}
state GetterInside reduce(), access current state via the state getter:
class ToggleFlag extends ReduxAction<AppState> {
@override
AppState? reduce() => state.copy(flag: !state.flag);
}
Pass data to actions through constructor fields:
class SetName extends ReduxAction<AppState> {
final String name;
SetName(this.name);
@override
AppState? reduce() => state.copy(name: name);
}
class IncrementBy extends ReduxAction<AppState> {
final int amount;
IncrementBy({required this.amount});
@override
AppState? reduce() => state.copy(counter: state.counter + amount);
}
For nested state objects, create the new nested object first:
class UpdateUserName extends ReduxAction<AppState> {
final String name;
UpdateUserName(this.name);
@override
AppState? reduce() {
var newUser = state.user.copy(name: name);
return state.copy(user: newUser);
}
}
Use context extensions:
// Fire and forget
context.dispatch(Increment());
// With parameters
context.dispatch(SetName('Alice'));
context.dispatch(IncrementBy(amount: 5));
Sync actions update state immediately:
print(store.state.counter); // 2
store.dispatch(IncrementBy(amount: 3));
print(store.state.counter); // 5
The dispatchSync() throws StoreException if the action is async. Otherwise, it
behaves exactly like dispatch().
Use dispatchSync() only in the rare cases when you must ensure the action is synchronous
because you need the state to be applied right after the dispatch returns.
context.dispatchSync(Increment());
Actions can dispatch other actions:
class ResetAndIncrement extends ReduxAction<AppState> {
@override
AppState? reduce() {
dispatch(Reset());
dispatch(Increment());
return null; // This action itself doesn't change state
}
}
Return null when you don't need to change state:
class LogCurrentState extends ReduxAction<AppState> {
@override
AppState? reduce() {
print('Current counter: ${state.counter}');
return null; // No state change
}
}
Conditional state changes:
class IncrementIfPositive extends ReduxAction<AppState> {
final int amount;
IncrementIfPositive(this.amount);
@override
AppState? reduce() {
if (amount <= 0) return null;
return state.copy(counter: state.counter + amount);
}
}
Create a base action class to reduce boilerplate:
// Define once
abstract class AppAction extends ReduxAction<AppState> {}
// Use everywhere
class Increment extends AppAction {
@override
AppState? reduce() => state.copy(counter: state.counter + 1);
}
class SetName extends AppAction {
final String name;
SetName(this.name);
@override
AppState? reduce() => state.copy(name: name);
}
You can add shared functionality to your base class:
abstract class AppAction extends ReduxAction<AppState> {
// Shortcuts to state parts
User get user => state.user;
Settings get settings => state.settings;
}
class UpdateEmail extends AppAction {
final String email;
UpdateEmail(this.email);
@override
AppState? reduce() => state.copy(
user: user.copy(email: email), // Uses shortcut
);
}
The reduce() method signature is FutureOr<AppState?>. For sync actions, always return
AppState? directly:
// CORRECT - Sync action
AppState? reduce() => state.copy(counter: state.counter + 1);
// WRONG - Don't return FutureOr directly
FutureOr<AppState?> reduce() => state.copy(counter: state.counter + 1);
If you return FutureOr<AppState?> directly, AsyncRedux cannot determine if the action is
sync or async and will throw a StoreException.
// State
class AppState {
final int counter;
final String name;
AppState({required this.counter, required this.name});
static AppState initialState() => AppState(counter: 0, name: '');
AppState copy({int? counter, String? name}) => AppState(
counter: counter ?? this.counter,
name: name ?? this.name,
);
}
// Base action
abstract class AppAction extends ReduxAction<AppState> {}
// Sync actions
class Increment extends AppAction {
@override
AppState? reduce() => state.copy(counter: state.counter + 1);
}
class Decrement extends AppAction {
@override
AppState? reduce() => state.copy(counter: state.counter - 1);
}
class IncrementBy extends AppAction {
final int amount;
IncrementBy(this.amount);
@override
AppState? reduce() => state.copy(counter: state.counter + amount);
}
class SetName extends AppAction {
final String name;
SetName(this.name);
@override
AppState? reduce() => state.copy(name: name);
}
class Reset extends AppAction {
@override
AppState? reduce() => AppState.initialState();
}
// Usage in widget
ElevatedButton(
onPressed: () => context.dispatch(IncrementBy(5)),
child: Text('Add 5'),
)
URLs from the documentation: