flutter_easybus

flutter 订阅通信总线

Getting Started

This project is a starting point for a Dart package, a library module containing code that can be shared easily across multiple Flutter or Dart projects.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

基础使用

定义任意订阅对象

class BusTest{  
 late Map bus;  
 BusTest({required this.bus});  
}

注册订阅

第一种(推荐)

EventSubscription subscription=  EasyBus().on<BusTest>().listen((arg) {  
   print("normal listen ${arg.bus}" );  
 });  
//subscription.free();///在合适的位置释放 防止内存泄露

第二种

/// 注意使用wait 方法订阅在第一次回调会自动销毁
var busTest= await EasyBus().on<BusTest>().wait();  
print("wait listen ${busTest.bus}");

发送信息

EasyBus().send(BusTest(bus: {'name':'easyBus'}));

注销所有当前对象订阅

EasyBus().free<BusTest>();

有没有更简单的使用方法

EasyBus().simpleOn().listen((arg) { ///直接使用内置对象
  print("simple listen ${arg.key}" );  
});

/// 发送信息
 EasyBus().simpleSend("key123", {'name':'lisa'});

Libraries

flutter_easybus