Files
xueguang_flutter_app/lib/global/event_bus.dart
2025-11-28 18:01:09 +08:00

34 lines
700 B
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:async';
/// 全局事件总线
class EventBus {
EventBus._();
static final _instance = EventBus._();
factory EventBus() => _instance;
// StreamController广播模式可以让多个地方监听
final StreamController<GlobalEvent> _controller = StreamController.broadcast();
/// 发送事件
void fire(GlobalEvent event) {
_controller.add(event);
}
/// 监听事件
Stream<GlobalEvent> get stream => _controller.stream;
/// 关闭流(一般应用生命周期结束时调用)
void dispose() {
_controller.close();
}
}
/// 事件类型枚举
enum GlobalEvent {
unauthorized, // 401 需要重新登录
// 以后可以扩展其他事件
}