没有合适的资源?快使用搜索试试~ 我知道了~
首页Flutter中EventBus的使用
Flutter中EventBus的使用
1000 浏览量
更新于2023-05-30
评论
收藏 36KB PDF 举报
参考文档:电梯直达 EventBusUtils //订阅者回调签名 typedef void EventCallback(arg); ///* 作者:guoyzh ///* 时间:2020年1月7日 ///* 功能:创建eventBus工具类 class EventBus { /// 私有构造函数 EventBus._internal(); /// 保存单例 static EventBus _singleton = new EventBus._internal(); /// 工厂构造函数 factory EventBus() => _singleton; /// 保
资源详情
资源评论
资源推荐

Flutter中中EventBus的使用的使用
参考文档:电梯直达
EventBusUtils
//订阅者回调签名
typedef void EventCallback(arg);
///* 作者:guoyzh
///* 时间:2020年1月7日
///* 功能:创建eventBus工具类
class EventBus {
/// 私有构造函数
EventBus._internal();
/// 保存单例
static EventBus _singleton = new EventBus._internal();
/// 工厂构造函数
factory EventBus() => _singleton;
/// 保存事件订阅者队列,key:事件名(id),value: 对应事件的订阅者队列
var _eMap = new Map<Object, List>();
/// 添加订阅者
void on(eventName, EventCallback f) {
if (eventName == null || f == null) return;
_eMap[eventName] ??= new List();
_eMap[eventName].add(f);
}
/// 移除订阅者
void off(eventName, [EventCallback f]) {
var list = _eMap[eventName];
if (eventName == null || list == null) return;
if (f == null) {
_eMap[eventName] = null;
} else {
list.remove(f);
}
}
/// 触发事件,事件触发后该事件所有订阅者会被调用
void send(eventName, [arg]) {
var list = _eMap[eventName];
if (list == null) return;
int len = list.length - 1;
//反向遍历,防止订阅者在回调中移除自身带来的下标错位
for (var i = len; i > -1; --i) {
list[i](arg);
}
}
}
订阅事件订阅事件
class RefundPage extends StatefulWidget {
RefundPage({this.title, this.name});
final String title;
final String name;
@override
_RefundPageState createState() => _RefundPageState();
}
class _RefundPageState extends State {
/// 获取全局单例的EventBus
var bus = EventBus();
// props 从 widget.xxx 获取



















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0