当前位置:网站首页>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 : '' }, }, );
边栏推荐
- 注册模块用例编写
- 音视频知识
- MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
- Interpretation of the standard of software programming level examination for teenagers_ second level
- JS judge the data types object.prototype.tostring.call and typeof
- 面试题目大赏
- Node 内存溢出及V8垃圾回收机制
- 电机转速模糊pid控制
- Login module use case writing
- 系统安装Serv-U后IIS出错提示:HRESULT:0x80070020
猜你喜欢

Interview shock 68: why does TCP need three handshakes?

一种分布式深度学习编程新范式:Global Tensor

在Blazor 中自定义权限验证

V-permission add permission

面试题目大赏

Gauss elimination solves the inverse of matrix (Gauss)

Source code analysis of object wait notify notifyAll

Does volatile rely on the MESI protocol to solve the visibility problem? (top)

附加到进程之后,断点显示“当前不会命中断点 还没有为该文档加载任何符号”

图解用户登录验证流程,写得太好了!
随机推荐
官方颁发的SSL证书与自签名证书结合实现网站双向认证
挡不住了,纯国产PC已就位,美国的软硬件体系垄断正式被破
SSG框架Gatsby访问数据库,并显示到页面上
Development to testing: a six-year road to automation starting from 0
POJ 1012 Joseph
2022年中科磐云——服务器内部信息获取 解析flag
Xiaobai makes a wave of deep copy and shallow copy
网站设计需要的基本知识
E. Two Small Strings
After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"
Drawing shadow error diagram with MATLAB
Calling DLL to start thread
Gauss elimination solves the inverse of matrix (Gauss)
添加dll
How to add a PDB
Registration module use case writing
copyTo
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
IIS website configuration
Force deduction brush questions, sum of three numbers