Flutter中EventBus组件的使用

本文介绍了Flutter中EventBus的使用步骤,包括导入event_bus包、创建EventBus工具类、定义事件类、发送事件以及监听并接收事件,着重于在页面间通信中的应用。

1.首先导入使用EventBus时所需的包:event_bus: ^1.1.1

2.还需要一个EventBus工具类:

import 'dart:async';

import 'package:event_bus/event_bus.dart';
import 'package:flutter/widgets.dart';

/// EventBus的工具类
class HosEventBusUtils {
  // 单列模式
  static EventBus _eventBus;

  static EventBus shared() {
    if (_eventBus == null) {
      _eventBus = EventBus(); // 创建事件总线
    }
    return _eventBus;
  }

  /// 订阅者
  static Map<Type, List<StreamSubscription>> subscriptions = {};

  /// 添加监听事件
  /// [T] 事件泛型 必须要传
  /// [onData] 接受到事件
  /// [autoManaged] 自动管理实例,off 取消
  static StreamSubscription on<T extends Object>(void onData(T event),
      {Function onError,
      void onDone(),
      bool cancelO
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值