1.首先导入使用EventBus时所需的包:event_bus: ^1.1.1
2.还需要一个EventBus工具类:
import 'dart:async';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/widgets.dart';
/// EventBus的工具类
class HosEventBusUtils {
// 单列模式
static EventBus _eventBus;
static EventBus shared() {
if (_eventBus == null) {
_eventBus = EventBus(); // 创建事件总线
}
return _eventBus;
}
/// 订阅者
static Map<Type, List<StreamSubscription>> subscriptions = {};
/// 添加监听事件
/// [T] 事件泛型 必须要传
/// [onData] 接受到事件
/// [autoManaged] 自动管理实例,off 取消
static StreamSubscription on<T extends Object>(void onData(T event),
{Function onError,
void onDone(),
bool cancelO
本文介绍了Flutter中EventBus的使用步骤,包括导入event_bus包、创建EventBus工具类、定义事件类、发送事件以及监听并接收事件,着重于在页面间通信中的应用。
订阅专栏 解锁全文
625

被折叠的 条评论
为什么被折叠?



