当前位置:网站首页>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 : '' }, }, );
边栏推荐
- CSV data file settings of JMeter configuration components
- (二)面扫描仪与机械臂的手眼标定(眼在手外:九点标定)
- 【荧光字效果】
- 解决npm -v突然失效 无反应
- MFC handy notes
- Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
- asp. Net using redis cache (2)
- Node memory overflow and V8 garbage collection mechanism
- 调用DLL开启线程的问题
- [MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
猜你喜欢
【荧光字效果】
2022 zhongkepan cloud - server internal information acquisition and analysis flag
开发转测试:从0开始的6年自动化之路...
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
Logical architecture of MySQL
Login module use case writing
Does volatile rely on the MESI protocol to solve the visibility problem? (top)
Registration module use case writing
matlab中的AR模型短时预测交通流
asp. Net using redis cache
随机推荐
音视频知识
R语言ggplot2可视化: 将图例标题(legend title)对齐到ggplot2中图例框的中间(默认左对齐、align legend title to middle of legend)
JS continuous assignment operation
微信小程序图片无法显示时显示默认图片
调用DLL开启线程的问题
阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践
学习笔记之常用数组api 改变原数组和不改变原数组的有哪些?
copyTo
Smart gourmet C language
在Blazor 中自定义权限验证
Search module use case writing
2019 ICPC Asia Yinchuan regional (water problem solution)
图解用户登录验证流程,写得太好了!
JS table auto cycle scrolling, mouse move in pause
Malloc failed to allocate space and did not return null
Node memory overflow and V8 garbage collection mechanism
Logical architecture of MySQL
The diagram of user login verification process is well written!
Double authentication of server and client
【荧光字效果】