当前位置:网站首页>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 : '' }, }, );
边栏推荐
- After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"
- (2) Hand eye calibration of face scanner and manipulator (eye out of hand: nine point calibration)
- V-for dynamically sets the SRC of img
- 2021 windows penetration of "Cyberspace Security" B module of Shandong secondary vocational group (analysis)
- MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
- 【信息系统项目管理师】初见高项系列精华汇总
- 服务器、客户端双认证
- 微信小程序AvatarCropper 头像裁剪
- EOJ 2020 1月月赛 E数的变换
- Audio and video knowledge
猜你喜欢

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

spolicy请求案例

配置ADCS后访问certsrv的问题

Search module use case writing

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

Server and client dual authentication (2)

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

After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"

MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具

Source code analysis of object wait notify notifyAll
随机推荐
Gauss elimination for solving XOR linear equations
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
R language ggpubr package ggsummarystats function visualizes the grouping box diagram (custom grouping color) and adds the statistical values corresponding to the grouping under the x-axis label (samp
面试突击68:为什么 TCP 需要 3 次握手?
如何添加一个PDB
Simple pedestrian recognition code to 88% accuracy Zheng Zhedong preparation
Solve NPM -v sudden failure and no response
图解用户登录验证流程,写得太好了!
“互联网+”时代的现代医学
Double authentication of server and client
高斯消元求解矩阵的逆(gauss)
Antd treeselect gets the value of the parent node
学习笔记之常用数组api 改变原数组和不改变原数组的有哪些?
音视频知识
Customize permission validation in blazor
在同一conda环境下先装Pytroch后装TensorFlow
Node 内存溢出及V8垃圾回收机制
The difference between thread join and object wait
Fiddler download and installation
系统安装Serv-U后IIS出错提示:HRESULT:0x80070020