Flutter EventBus事件总线的应用

Stream on() {
if (T == dynamic) {
return streamController.stream as Stream;
} else {
return streamController.stream.where((event) => event is T).cast();
}
}


## EventBus的实际应用



> 
> 1、在pubspec.yaml文件中引用eventBus事件总线依赖;  
>  2、创建一个全局的EventBus实例;  
>  3、使用fire(event)方法在事件总线上触发一个新事件(触发事件);  
>  4、为事件总线注册一个监听器(监听事件);  
>  5、取消EventBus事件订阅,防止内存泄漏。
> 
> 
> 



// 1、在pubspec.yaml文件中引用eventBus事件总线依赖;
dependencies:
event_bus: ^2.0.0



// 2、创建一个全局的EventBus实例;
EventBus myEventBus = EventBus();



// 3、使用fire(event)方法在事件总线上触发一个新事件(触发事件);
Center(
child: ElevatedButton(
onPressed: () {
myEventBus.fire(‘通过EventBus触发事件’);
},
child: Text(‘触发事件’),
),
)



var getData;

@override
void initState() {
// TODO: implement initState
super.initState();
// 4、为事件总线注册一个监听器(监听事件);
getData = myEventBus.on().listen((event) {
print(event);
});
}

@override
void dispose() {
// TODO: implement dispose
super.dispose();
// 5、取消EventBus事件订阅,防止内存泄漏。
getData.cancel();
}


## 总结


EventBus遵循的是发布/订阅模式,能够通过事件的触发和监听操作,有效实现跨组件通讯的功能。



> 
> 作者:Zheng  
>  链接:https://juejin.cn/post/7137327139061727262
> 
> 
> 


## 学习福利


### **《Flutter技术解析与实战》**


#### 目录


​ ![img](https://img-blog.csdnimg.cn/img_convert/19e63c497cbaa95d2c8c35e30b8109cf.png)


#### 第一章 混合工程


* Flutter工程体系
* 混合工程改造实战
* 混合工程与持续集成
* 快速完成混合工程搭建
* 使用混合栈框架开发


​ ![img](https://img-blog.csdnimg.cn/img_convert/6835b362052d8e7bdcccfc535f501ebc.png)


#### **第二章 能力增强**


* 基于原生能力的插件扩展
* 基于外接纹理的同层渲染
* 多媒体能力扩展实践
* 富文本能力应用实践


​ ![img](https://img-blog.csdnimg.cn/img_convert/b408f901c3bf79a3893180eece4ceb5f.png)


#### **第三章 业务架构设计**


* 应用框架设计实践
* 轻量级动态化渲染引擎的设计
* 面向切面编程的设计实践
* 高性能的动态模板渲染实践


​ ![img](https://img-blog.csdnimg.cn/img_convert/7e77afa5342eec08f97a5b9ddb0cbdfb.png)


#### **第四章 数据统计与性能**


* 数据统计框架的设计
* 性能稳定性监控方案的设计
* 高可用框架的设计与实践
* 跨端方案性能对比实践


​ ![img](https://img-blog.csdnimg.cn/img_convert/92a61b5971b6ed040c7cd0377e5eb880.png)


#### **第五章 企业级应用实战**


* 基于Flutter的端结构演进与创新
* Flutter与FaaS云端一体化架构


​ ![img](https://img-blog.csdnimg.cn/img_convert/5b9a0fca4f375ce2b96d3e19687e8268.png)


![img](https://img-blog.csdnimg.cn/img_convert/42e61429150b8c23c25fd91f5a4fd1a7.png)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值