当前位置:网站首页>Flutter event distribution
Flutter event distribution
2022-07-26 09:44:00 【nicepainkiller】
stay Other front-end development , To decouple Will use the event center , To distribute the event ; Flutter in You can also do this ;
But what? There is a market for plug-ins called event_bus Of ; It doesn't feel very good !
The place follows the previous train of thought ; Moved one !
Reference resources :https://www.jianshu.com/p/d036ed61f1c8 ; But it's probably an old version ;
This is optimized as follows :
typedef void EventCallback(arg); class AppEvent { static final AppEvent _instance = AppEvent._internal(); AppEvent._internal(); factory AppEvent() { return _instance; } // Save the event subscriber queue ,key: Event name (id),value: Subscriber queue corresponding to the event final _eMap = <dynamic, List<EventCallback>>{}; // Add subscribers void on(eventName, EventCallback callBack) { if (eventName == null) return; if (!_eMap.containsKey(eventName)) { _eMap.addEntries({eventName: <EventCallback>[]}.entries); } _eMap[eventName]?.add(callBack); } // Remove subscribers 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); } } // Triggering event , After the event is triggered, all subscribers of the event will be called void emit(eventName, [arg]) { var list = _eMap[eventName]; if (list == null) { print('** No corresponding subscription method found $eventName'); return; } int len = list.length - 1; // Reverse traversal , Prevent subscribers from removing subscript misalignment caused by themselves in the callback for (var i = len; i > -1; --i) { list[i](arg); } } }
- Add listening :
AppEvent().on('pageReport-examine', (arg) { print('>>>>> Received a message $arg'); setState(() { _examineListSelect.add(arg); }); });- Cancel monitoring :
AppEvent().off('pageReport-examine');- Trigger function :
AppEvent().emit( 'pageReport-examine', { 'id': '110', 'patrolRecordId': 'id', 'routeSubId': 'id', 'placeName': ' Test node ', '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 : '' }, }, );
边栏推荐
- 解释一下自动装箱和自动拆箱?
- Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
- Table extraction for opencv table recognition (2)
- v-for动态设置img的src
- Sqoop【付诸实践 02】Sqoop1最新版 全库导入 + 数据过滤 + 字段类型支持 说明及举例代码(query参数及字段类型强制转换)
- [MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
- 解决ProxyError: Conda cannot proceed due to an error in your proxy configuration.
- 2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
- Mo team learning summary (II)
- QT随手笔记(六)——更新界面、截图、文件对话框
猜你喜欢

Xiaobai makes a wave of deep copy and shallow copy

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

Search module use case writing

MySQL的逻辑架构

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

CSV data file settings of JMeter configuration components

2021年山东省中职组“网络空间安全”B模块windows渗透(解析)

Node 内存溢出及V8垃圾回收机制

阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践

Development to testing: a six-year road to automation starting from 0
随机推荐
Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
The whole process of server environment configuration
Learning notes: what are the common array APIs that change the original array or do not change the original array?
ie7设置overflow属性失效解决方法
QT handy notes (VI) -- update interface, screenshot, file dialog box
QT handy notes (III) use qtcharts to draw a line chart in VS
[MySQL] understand the important architecture of MySQL (I)
Interview shock 68: why does TCP need three handshakes?
服务器、客户端双认证
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
【荧光字效果】
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
Xiaobai makes a wave of deep copy and shallow copy
m进制数str转n进制数
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
高斯消元求解矩阵的逆(gauss)
Fiddler packet capturing tool for mobile packet capturing
Malloc failed to allocate space and did not return null