当前位置:网站首页>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 : '' }, }, );
边栏推荐
- 面试题目大赏
- 高斯消元的应用
- 电机转速模糊pid控制
- Learning notes: what are the common array APIs that change the original array or do not change the original array?
- antd TreeSelect获取父节点的值
- Process32first returns false, error x message 24
- Force deduction brush questions, sum of three numbers
- RMQ学习笔记
- Node 内存溢出及V8垃圾回收机制
- Basic knowledge of website design
猜你喜欢

Gauss elimination solves the inverse of matrix (Gauss)

正则表达式

2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
![[MySQL] understand the important architecture of MySQL (I)](/img/89/5fb595b0112fac987626857b76f9a4.png)
[MySQL] understand the important architecture of MySQL (I)

Windows下Redis哨兵模式搭建

【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)

spolicy请求案例

解决ProxyError: Conda cannot proceed due to an error in your proxy configuration.

图解用户登录验证流程,写得太好了!

面试突击68:为什么 TCP 需要 3 次握手?
随机推荐
The whole process of server environment configuration
莫队学习笔记(一)
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
Fuzzy PID control of motor speed
[Online deadlock analysis] by index_ Deadlock event caused by merge
JS judge the data types object.prototype.tostring.call and typeof
How to add a PDB
Smart gourmet C language
Node 内存溢出及V8垃圾回收机制
服务器环境配置全过程
Simple pedestrian recognition code to 88% accuracy Zheng Zhedong preparation
2021年山东省中职组“网络空间安全”B模块windows渗透(解析)
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"
R语言ggpubr包ggsummarystats函数可视化分组箱图(自定义分组颜色)并在X轴标签下方添加分组对应的统计值(样本数N、中位数median、四分位数的间距iqr、统计值的色彩和分组图色匹配
QT handy notes (VI) -- update interface, screenshot, file dialog box
dll中的全局变量
EOJ 2020 1月月赛 E数的变换
MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具