当前位置:网站首页>Flutter Event 派发
Flutter Event 派发
2022-07-26 09:40:00 【nicepainkiller】
在 其他前端开发中,为了解耦 会使用事件中心,进行事件的派发; Flutter中 也可以这么操作;
但是呢 插件市场有个叫 event_bus的;感觉不怎么好用!
所在照着以前的思路;搬了一个!
参考:https://www.jianshu.com/p/d036ed61f1c8 ;但是估计是个老版本的;
这边优化了一下 如下:
typedef void EventCallback(arg); class AppEvent { static final AppEvent _instance = AppEvent._internal(); AppEvent._internal(); factory AppEvent() { return _instance; } //保存事件订阅者队列,key:事件名(id),value: 对应事件的订阅者队列 final _eMap = <dynamic, List<EventCallback>>{}; //添加订阅者 void on(eventName, EventCallback callBack) { if (eventName == null) return; if (!_eMap.containsKey(eventName)) { _eMap.addEntries({eventName: <EventCallback>[]}.entries); } _eMap[eventName]?.add(callBack); } //移除订阅者 void off(eventName, {EventCallback? callBack}) { var list = _eMap[eventName]; if (eventName == null || list == null) return; if (callBack == null) { // _eMap[eventName] = null; _eMap[eventName]?.clear(); } else { list.remove(callBack); } } //触发事件,事件触发后该事件所有订阅者会被调用 void emit(eventName, [arg]) { var list = _eMap[eventName]; if (list == null) { print('**没有找打对应的订阅方法$eventName'); return; } int len = list.length - 1; //反向遍历,防止订阅者在回调中移除自身带来的下标错位 for (var i = len; i > -1; --i) { list[i](arg); } } }
- 添加监听:
AppEvent().on('pageReport-examine', (arg) { print('>>>>>收到消息$arg'); setState(() { _examineListSelect.add(arg); }); });
- 取消监听:
AppEvent().off('pageReport-examine');
- 触发函数:
AppEvent().emit( 'pageReport-examine', { 'id': '110', 'patrolRecordId': 'id', 'routeSubId': 'id', 'placeName': '测试节点', 'checkTime': '', 'isChecked': false, 'checkRecordInfo': { 'trouble': false, 'address': _locationResult?['address'], 'longitude': _locationResult?['latitude'], 'latitude': _locationResult?['longitude'], 'imgs': _selectPicture, 'voicePath': _selectAudio.isNotEmpty ? _selectAudio.first : '', 'bak': '', 'videoPath': _selectVideo.isNotEmpty ? _selectVideo.first : '' }, }, );
边栏推荐
- 【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)
- Solve NPM -v sudden failure and no response
- Interpretation of the standard of software programming level examination for teenagers_ second level
- Gauss elimination
- Add DLL
- 高斯消元
- 2020-12-29
- Node memory overflow and V8 garbage collection mechanism
- 一种分布式深度学习编程新范式:Global Tensor
- SSG框架Gatsby访问数据库,并显示到页面上
猜你喜欢
Login module use case writing
注册模块用例编写
OFDM Lecture 16 - OFDM
面试突击68:为什么 TCP 需要 3 次握手?
Fiddler download and installation
MySQL的逻辑架构
2019 ICPC Asia Yinchuan regional (water problem solution)
Source code analysis of object wait notify notifyAll
Drawing shadow error diagram with MATLAB
The problem of accessing certsrv after configuring ADCs
随机推荐
malloc分配空间失败,并且不返回null
IIS error prompt after installing Serv-U: hresult:0x80070020
小白搞一波深拷贝 浅拷贝
QT handy notes (VI) -- update interface, screenshot, file dialog box
ie7设置overflow属性失效解决方法
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
AR model in MATLAB for short-term traffic flow prediction
Redis sentinel mode setup under Windows
How to add a PDB
(1) Hand eye calibration of face scanner and manipulator (eye on hand)
Qt随手笔记(二)Edit控件及float,QString转化、
新公链Aptos何以拉满市场期待值?
V-permission add permission
asp.net 使用redis缓存(二)
(2) Hand eye calibration of face scanner and manipulator (eye out of hand: nine point calibration)
Double authentication of server and client
2022年中科磐云——服务器内部信息获取 解析flag
JS 连等赋值操作
“互联网+”时代的现代医学
The problem of accessing certsrv after configuring ADCs