当前位置:网站首页>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 : '' }, }, );
边栏推荐
猜你喜欢
OFDM Lecture 16 - OFDM
Login module use case writing
Redis sentinel mode setup under Windows
电机转速模糊pid控制
【Datawhale】【机器学习】糖尿病遗传风险检测挑战赛
spolicy请求案例
Due to fierce competition in the new market, China Mobile was forced to launch a restrictive ultra-low price 5g package
Customize permission validation in blazor
Source code analysis of object wait notify notifyAll
【荧光字效果】
随机推荐
E. Two Small Strings
【信息系统项目管理师】初见高项系列精华汇总
添加dll
MySQL的逻辑架构
服务器、客户端双认证
新公链Aptos何以拉满市场期待值?
matlab中的AR模型短时预测交通流
Simple pedestrian recognition code to 88% accuracy Zheng Zhedong preparation
antd TreeSelect获取父节点的值
面试题目大赏
Redis sentinel mode setup under Windows
IIS website configuration
Azkaban【基础知识 01】核心概念+特点+Web界面+架构+Job类型(一篇即可入门Azkaban工作流调度系统)
高斯消元
asp. Net using redis cache (2)
Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken
Process32First返回false,错误x信息24
高斯消元的应用
QT handy notes (II) edit control and float, qstring conversion
OFDM Lecture 16 - OFDM