flutter EventBus

#配置文件中 先 引入

event_bus: ^2.0.0

1.封装一个类 便于直接调用
Dart 复制代码
import 'package:event_bus/event_bus.dart';
class EvenBlueUtil{

  static EventBus eventBus = EventBus();

  static EventBus eventBusUpdate = EventBus();

}

class UserLoggedInEvent {

  var user;

  UserLoggedInEvent(this.user);

}

class NewOrderEvent {

  var order;
  NewOrderEvent(this.order);

}
2.使用方法
Dart 复制代码
//可以传值也可以不传值 括号类型可以放入自己想要的信息
EvenBlueUtil.eventBus.fire(null);
3.在需要接受页面的消息进行注册

StreamSubscription? subscription;

从even可以接收传递的信息

Dart 复制代码
 @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController(
    
    subscription = EvenBlueUtil.eventBus.on().listen((event) {
      
      print("消费!!!!!!");
      
     
    });
  }

  @override
  void dispose() {
    
    super.dispose();
    subscription!.cancel();
  }

//注意 离开页面会被销毁,会接收不到 但是跳转页面不会,比如跳转页面进行修改,然后返回页面是可以的,但是 点击底部菜单栏从 首页,我的来回切换是不可以的

相关推荐
00后程序员张3 小时前
iOS应用性能优化全解析:卡顿、耗电、启动与瘦身
android·ios·性能优化·小程序·uni-app·iphone·webview
Rsun045513 小时前
16、Java 迭代器模式从入门到实战
java·开发语言·迭代器模式
quan26313 小时前
20260416,日常开发-再记一次内存溢出
java·内存溢出·jprofile
布吉岛的石头3 小时前
线上服务凌晨OOM:一次因「无超时设置」引发的内存雪崩复盘
java
SamDeepThinking3 小时前
Spring Bean作用域的设计与使用
java·后端·面试
Flittly3 小时前
【SpringSecurity新手村系列】(2)整合 MyBatis 实现数据库认证
java·安全·spring·springboot·安全架构
程序员陆业聪3 小时前
跨端框架横评 2026:Flutter、React Native、KMP、小程序,谁是你下一个项目的正确答案?
android
aq55356003 小时前
Laravel3.x核心特性全解析
android
菜鸟国国3 小时前
Compose 点击/按压事件全解析:从基础到进阶,新手也能秒懂
android
Oliver_LaVine4 小时前
java项目启动报错:CreateProcess error=206, 文件名或扩展名太长
java·linux·jenkins