with one click
fx-event-usage
使用 fx_event 进行类型化事件通信。适用于回调层级过深、跨模块通信、远距离组件交互、异步请求-响应等场景。
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Menu
使用 fx_event 进行类型化事件通信。适用于回调层级过深、跨模块通信、远距离组件交互、异步请求-响应等场景。
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Based on SOC occupation classification
| name | fx-event-usage |
| description | 使用 fx_event 进行类型化事件通信。适用于回调层级过深、跨模块通信、远距离组件交互、异步请求-响应等场景。 |
| metadata | {"author":"toly","version":"0.0.1","tags":["fx","event","bus","emitter","async","flutter"]} |
fx_event: 0.0.1
检查项目是否包含 fx_event 依赖。
| 场景 | 问题 | fx_event 解法 |
|---|---|---|
| 回调层级过深 | Widget 嵌套 5+ 层传回调,中间层被迫透传 | 深层直接 emit(),顶层 on<E>() 接收 |
| 跨模块通信 | A 模块需要通知 B 模块,但两者无直接依赖 | 双方只依赖 fx_event,通过事件类型解耦 |
| 距离很远的交互 | 底部 Tab 点击需要刷新另一个 Tab 的列表 | 发送 RefreshEvent,目标页 mixin 监听 |
| 请求-响应 | 业务层需要 UI 确认(弹窗/选择器)再继续 | AsyncFxEvent 发送等待,UI 层 complete |
| 概念 | 说明 |
|---|---|
| FxEvent | 事件基类,所有自定义事件继承它 |
| AsyncFxEvent<T> | 异步事件,发送方可等待处理结果 |
| FxEmitter | 全局事件总线单例 |
| FxEmitterMixin | 监听所有事件的 State mixin |
| FxSingleEventMixin | 只监听指定类型事件的 State mixin |
1. 定义事件类 → 继承 FxEvent 或 AsyncFxEvent<T>
2. 注册监听 → FxEmitter().on<E>() 或 State mixin
3. 发送事件 → event.emit() 或 emitAsync()
4. 处理结果 → 异步事件通过 complete 返回
继承 FxEvent 定义同步事件,继承 AsyncFxEvent<T> 定义异步事件。
#[[file:references/define_event.md]]
通过 FxEmitter().on<E>() 按类型监听,通过 event.emit() 发送。
#[[file:references/emit_listen.md]]
发送方 emitAsync() 等待,处理方 complete(result) 返回结果。
#[[file:references/async_event.md]]
在 StatefulWidget 中使用 mixin 自动管理订阅生命周期。
#[[file:references/state_mixin.md]]
| API | 作用 |
|---|---|
event.emit() | 发送事件到总线 |
FxEmitter().on<E>(handler) | 按类型监听 |
FxEmitter().stream | 监听所有事件 |
asyncEvent.emitAsync() | 发送并等待结果 |
asyncEvent.emitAsync(timeout: ...) | 带超时的异步发送 |
asyncEvent.complete(result) | 处理方完成事件 |
asyncEvent.completeError(error) | 处理方以错误完成 |
stream.whereType<S>() | Stream 类型过滤扩展 |
const 构造(如果字段都是 final)XxxEvent 后缀使用 fx_boot_starter 接入 App 启动流程。适用于需要闪屏页、初始化异步任务、启动状态管理的场景。
使用 fx_dio 进行 Flutter HTTP 请求开发。基于实际项目实践的模式,包含 Host 定义、请求发起、返回值处理、异常追踪等核心流程。
使用 fx_exception 构建统一异常追踪体系。提供 Code、Trace、TraceMixin 三个核心协议,各模块基于此扩展自定义异常。
使用 fx_trace 进行应用级异常/日志追踪。统一收集网络异常、业务提示、调试日志,支持级别过滤和 State 生命周期绑定。