美文网首页
flutter event_bus

flutter event_bus

作者: _诸葛青 | 来源:发表于2021-06-07 15:38 被阅读0次
  #事件监听
  event_bus: ^1.1.1

事件代码

import 'package:event_bus/event_bus.dart';
class TestEventBus {
  static final TestEventBus _gInstance = TestEventBus._init();

  EventBus _eventBus = EventBus();

  TestEventBus._init() {
    ///
  }

  factory TestEventBus() {
    return _gInstance;
  }

  EventBus get bus {
    return _eventBus;
  }
}

使用:

创建事件
class ProgressEvents {
  String data;///传输数据就在这里加
  ProgressEvents({this.data});
}
触发的地方:
     ProgressEventBus().bus.fire(ProgressEvents(data: ''));
监听的地方:
    ProgressEventBus().bus.on<ProgressEvents>().listen((event) {
      if (!mounted) return;

    });
///取消订阅 
  StreamSubscription subscription;
subscription=ProgressEventBus().bus.on<ProgressEvents>().listen((event) {
      if (!mounted) return;

    });
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    if (subscription != null) {
      subscription.cancel();
    }
  }

注:监听应比触发早。

相关文章

网友评论

      本文标题:flutter event_bus

      本文链接:https://www.haomeiwen.com/subject/djmleltx.html