当前位置:网站首页>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 : '' }, }, );
边栏推荐
- m进制数str转n进制数
- asp.net 使用redis缓存(二)
- Qt随手笔记(三)在vs中使用QtCharts画折线图
- QT handy notes (II) edit control and float, qstring conversion
- Neural network and deep learning-6-support vector machine 1-pytorch
- R language ggplot2 visualization: align the legend title to the middle of the legend box in ggplot2 (default left alignment, align legend title to middle of legend)
- Redis sentinel mode setup under Windows
- opencv 类的使用
- Source code analysis of object wait notify notifyAll
- 阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
猜你喜欢

AR model in MATLAB for short-term traffic flow prediction

Spolicy request case

面试题目大赏

【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛

高斯消元

苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收

QT handy notes (III) use qtcharts to draw a line chart in VS

Development to testing: a six-year road to automation starting from 0

开发转测试:从0开始的6年自动化之路...

【荧光字效果】
随机推荐
The diagram of user login verification process is well written!
A new paradigm of distributed deep learning programming: Global tensor
C managed and unmanaged
高斯消元求解异或线性方程组
The combination of officially issued SSL certificate and self signed certificate realizes website two-way authentication
Basic knowledge of website design
Network flow learning notes
V-for dynamically sets the SRC of img
E. Two Small Strings
图解用户登录验证流程,写得太好了!
uni-app学习总结
[Online deadlock analysis] by index_ Deadlock event caused by merge
服务器、客户端双认证(2)
E. Two Small Strings
SSG框架Gatsby访问数据库,并显示到页面上
Force deduction brush questions, sum of three numbers
Wechat applet avatarcropper avatar clipping
苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
配置ADCS后访问certsrv的问题
2020-12-29