当前位置:网站首页>Flutter adds event listening | subscription
Flutter adds event listening | subscription
2022-06-25 10:23:00 【Crazy fox 12138】
The singleton pattern
// Subscriber callback signature
typedef void EventCallback(arg);
class EventBus {
// Private constructor
EventBus._internal();
// Save single example
static EventBus _singleton = EventBus._internal();
// Factory constructor
factory EventBus()=> _singleton;
// Save the event subscriber queue ,key: Event name (id),value: Subscriber queue corresponding to the event
final _emap = Map<Object, List<EventCallback>?>();
// Add subscribers
void on(eventName, EventCallback f) {
_emap[eventName] ??= <EventCallback>[];
_emap[eventName]!.add(f);
}
// Remove subscribers
void off(eventName, [EventCallback? f]) {
var list = _emap[eventName];
if (eventName == null || list == null) return;
if (f == null) {
_emap[eventName] = null;
} else {
list.remove(f);
}
}
// 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) 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);
}
}
}
// Define a top-level( overall situation ) Variable , The page can be used directly after importing the file bus
var bus = EventBus();
Examples of use
// page A in
...
// Listen for login events
bus.on("login", (arg) {
// do something
});
// The login page B in
...
// The login event will be triggered after successful login , page A Subscribers in will be called
bus.emit("login", userInfo);
```ji
边栏推荐
- Neat Syntax Design of an ETL Language (Part 2)
- 【论文阅读|深度】Role-based network embedding via structural features reconstruction with degree-regularized
- Modbus protocol and serialport port read / write
- Handler asynchronous message processing
- i++ 和 ++i的真正区别
- BUG-00x bug description + resolve ways
- JS tool function, self encapsulating a throttling function
- OpenCV学习(二)---树莓派上安装opencv
- tokenizers>=0.11.1,!= 0.11.3,<0.13 is required for a normal functioning of this module,
- Learning notes of rxjs takeuntil operator
猜你喜欢
String implementation strstr()
Create menu file
字符串 实现 strStr()
I hope to explain the basics of canvas as clearly as possible according to my ideas
The title of my composition is - "my district head father"
什么是 CRA
Floating window --- create an activity floating window (can be dragged)
Principle of distribution: understanding the gossip protocol
Flask blog practice - realize the latest articles and search in the sidebar
ScheduleMaster分布式任务调度中心基本使用和原理
随机推荐
Handler asynchronous message processing
我希望按照我的思路盡可能將canvas基礎講明白
How much does a small program cost? How much does a small program cost? It's clear at a glance
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
Redis (I) principle and basic use
WebApi性能优化
Is GF Securities reliable? Is it legal? Is it safe to open a stock account?
输出式阅读法:把学到的知识用起来
Repo sync will automatically switch the correspondence between the local branch and the remote branch - how to customize this behavior
一文了解Prometheus
Shardingsphere proxy 5.0 sub database and sub table (I)
Kotlin advanced set
Oracle query comes with JDK version
How do wechat applets make their own programs? How to make small programs on wechat?
Redis(一)原理与基本使用
Difference between malloc and calloc
JS【中高级】部分的知识点我帮你们总结好了
Basic use and cluster construction of consult
Flask blog practice - realize personal center and authority management
MySQL create given statement