当前位置:网站首页>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
边栏推荐
- [200 opencv routines] 210 Are there so many holes in drawing a straight line?
- Computational Thinking and economic thinking
- I have summarized the knowledge points of JS [intermediate and advanced] for you
- Bug- solve the display length limitation of log distinguished character encoding (edittext+lengthfilter)
- Macro application connector\
- 我希望按照我的思路尽可能将canvas基础讲明白
- OODA工作法
- 【动态规划】—— 数字三角形
- Kotlin arrays and collections (1) {create arrays, use arrays, use for in loops to traverse arrays, use array indexes, and multi-dimensional arrays}
- 西门子PLCS7-200使用(一)---开发环境和组态软件入门
猜你喜欢

Jetpack compose layout (I) - basic knowledge of layout

Jetpack compose layout (II) - material components and layout

我希望按照我的思路尽可能将canvas基础讲明白

Houdini图文笔记:Could not create OpenCL device of type (HOUDINI_OCL_DEVICETYPE)问题的解决

How to apply for a widget on wechat how to get a widget on wechat

Linked list delete nodes in the linked list

Jetpack compose layout (III) - custom layout

String longest common prefix

Free applet making tool, how to make wechat applet

How do wechat sell small commodity programs do? How to open wechat apps to sell things?
随机推荐
WebApi性能优化
Webapi performance optimization
Kotlin keyword and operator
How to do the wechat selling applet? How to apply for applets
Flask博客实战 - 实现个人中心及权限管理
Houdini图文笔记:Your driver settings have been set to force 4x Antialiasing in OpenGL applications问题的解决
好好思考
Guiding principle - read source code
Neat Syntax Design of an ETL Language (Part 2)
IdentityServer4 定义概念
ShardingSphere-Proxy 5.0 分库分表(一)
How to apply for a widget on wechat how to get a widget on wechat
Flask博客实战 - 实现侧边栏最新文章及搜索
Mqtt beginner level chapter
I have summarized the knowledge points of JS [intermediate and advanced] for you
Mongodb's principle, basic use, clustering and partitioned clustering
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
[dynamic planning] - Digital triangle
Repo sync will automatically switch the correspondence between the local branch and the remote branch - how to customize this behavior
在Microsoft Exchange Server 2007中安装SSL证书的教程