{"id":15293152,"url":"https://github.com/timnew/flutter_event_bus","last_synced_at":"2025-04-13T12:29:02.685Z","repository":{"id":36475349,"uuid":"227365332","full_name":"timnew/flutter_event_bus","owner":"timnew","description":"Flutter Event Bus is an EventBus designed specific for Flutter app, which enable developer to write flutter app with Interactor pattern","archived":false,"fork":false,"pushed_at":"2021-11-16T06:37:44.000Z","size":7431,"stargazers_count":15,"open_issues_count":4,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-27T03:34:42.455Z","etag":null,"topics":["bloc","clean-architecture","dart","dartlang","event-bus","eventbus","flutter","interactor","library","mobile","mobile-app"],"latest_commit_sha":null,"homepage":"https://github.com/timnew/flutter_event_bus","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/timnew.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-11T12:51:54.000Z","updated_at":"2023-05-02T22:06:17.000Z","dependencies_parsed_at":"2022-08-22T18:50:20.382Z","dependency_job_id":null,"html_url":"https://github.com/timnew/flutter_event_bus","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fflutter_event_bus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fflutter_event_bus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fflutter_event_bus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timnew%2Fflutter_event_bus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timnew","download_url":"https://codeload.github.com/timnew/flutter_event_bus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248713780,"owners_count":21149773,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bloc","clean-architecture","dart","dartlang","event-bus","eventbus","flutter","interactor","library","mobile","mobile-app"],"created_at":"2024-09-30T16:40:00.809Z","updated_at":"2025-04-13T12:29:02.655Z","avatar_url":"https://github.com/timnew.png","language":"Dart","readme":"# flutter_event_bus\n\n[![Star this Repo](https://img.shields.io/github/stars/timnew/flutter_event_bus.svg?style=flat-square)](https://github.com/timnew/flutter_event_bus)\n[![Pub Package](https://img.shields.io/pub/v/flutter_event_bus.svg?style=flat-square)](https://pub.dev/packages/flutter_event_bus)\n\nFlutter Event Bus is an `EventBus` designed specific for Flutter app, which enable developer to write flutter app with Interactor pattern, which is similar to Bloc but less structured on some aspect.\n\n## Why another Event Bus\n\nThere is a [Event Bus](https://pub.dev/packages/event_bus) package, why create another one?\n\nMarco Jakob did a great job while creating `Event Bus` package, which provides a generic Event Bus pattern implementation that can be used anywhere in Dart ecosystem.\n\nBut while writing app in Interactor pattern in flutter, there are a few common usages that existing library are not really convenient. So `Flutter Event Bus` has been carefully customised for these use cases.\n\n## Event Bus\n\nEvent Bus is a pub/sub system to enable components collaborate with each other without direct coupling. Component can publish event to make announcement when something happens, or respond to event to take action.\n\n## Basic Usage\n\n```dart\nclass TextChangedEvent{\n  final String text;\n  const TextChangedEvent(this.text);\n}\n\nfinal eventBus = EventBus();\n\neventBus.respond\u003cTextChangedEvent\u003e((TextChangedEvent event) =\u003e\n  print(\"Text changed to ${event.text}\");\n);\n\neventBus.publish(TextChangedEvent(\"new text\"));\n```\n\n### Stop responding events\n\n```dart\nfinal subscription = eventBus.respond\u003cTextChangedEvent\u003e(responder);\n\neventBus.publish(TextChangedEvent(\"Responded\")); // This event will be responded by responder\n\nsubscription.dispose();\n\neventBus.publish(TextChangedEvent(\"Ignored\")); // This event will not be responded by responder\n```\n\n### Respond to different type of events\n\n```dart\nfinal subscription = eventBus\n  .respond\u003cEventA\u003e(responderA) // Subscribe to EventA\n  .respond\u003cEventB\u003e(responderB) // Subscribe to EventB\n  .respond\u003cEventC\u003e(responderC) // Subscribe to EventC\n  .respond(genericResponder); // Subscribe to EventA EventB EventC and any other event on event bus\n\n  // Generic Responder could be useful for monitoring, logging or diagnosis purpose, probably will be hardly used to take action to event\n\nsubscription.dispose(); // All previous 4 subscriptions will be cancelled all together\n```\n\n## Used in Flutter\n\n### Event Bus Widget\n\nTo embed `EventBus` in Flutter, you can use `EventBusWidget`, which provide `EventBus` to its child tree.\n\n```dart\nclass MyApp extends StatelessWidget {\n  // This widget is the root of your application.\n  @override\n  Widget build(BuildContext context) =\u003e\n    EventBusWidget(\n      child: MaterialApp(\n        // .....\n      )\n    );\n}\n```\n\n### Capture user interaction\n\nYou're like to publish event in stateless widget to broadcast user interaction into your app.\n\n```dart\nclass SubmitFormEvent { }\n\nclass SubmitButton extends StatelessWidget {\n  @override\n  Widget build(BuildContext context) =\u003e\n    FlatButton(\n      child: const Text(\"Publish\"),\n      onPressed: () {\n        EventBus.publish(context, SubmitFormEvent()); // Publish event to the event bus provided by ancestor EventBusWidget\n      }\n    )\n}\n```\n\n### Capture state changes\n\nYou might also wish to publish some event when app state had a certain change\n\n```dart\nclass MyWidgetState extends State\u003cMyWidget\u003e {\n  int _counter = 0;\n  EventBus eventBus;\n\n  @override\n  void didChangeDependencies() {\n    super.didChangeDependencies();\n\n    eventBus = EventBus.of(context); // Find EventBus provided by ancestor EventBusWidget\n  }\n\n\n  void _incrementCounter(InreaseCounterEvent event) {\n    setState(() {\n      if(++_counter == 100) {\n        eventBus.publish(CounterMaximizedEvent());\n      }\n    });\n  }\n}\n```\n\n### Respond events\n\nTo respond to events you can listening event bus directly. But more commonly you will do it via `Interactor`.\n\nYou can use `Interactor` as base class as your stateful widget state, and implements the `subscribeEvents` method to describe the events that interactor can handle. Interactor manages the subscription and cancel the subscription when it is removed from the tree.\n\n```dart\nclass IncreaseCounterEvent {}\nclass DecreaseCounterEvent {}\nclass CounterChangedEvent {\n  final int value;\n  CounterChangedEvent(this.value);\n}\n\nclass MyPage extends StatefulWidget {\n  MyPage({Key key}) : super(key: key);\n\n  @override\n  _MyPageInteractor createState() =\u003e _MyPageInteractor();\n}\n\nclass _MyPageInteractor extends Interactor\u003cMyHomePage\u003e {\n  int _counter = 0;\n\n  @override\n  Widget build(BuildContext context) {\n    // Build the widget tree as usual\n  }\n\n  @override\n  Subscription subscribeEvents(EventBus eventBus) =\u003e eventBus\n    .respond\u003cIncreaseCounterEvent\u003e(_incrementCounter)\n    .respond\u003cDecreaseCounterEvent\u003e(_decrementCounter);\n\n  void _incrementCounter(IncreaseCounterEvent event) {\n    setState(() {\n      _counter++;\n\n      eventBus.publish(CounterChangedEvent(_counter));\n    });\n  }\n\n   void _decrementCounter(DecreaseCounterEvent event) {\n    setState(() {\n      _counter--;\n\n      if(_counter \u003c 0) {\n        _counter = 0;\n        eventBus.publish(CounterReachZeroEvent());\n      }\n\n      eventBus.publish(CounterChangedEvent(_counter));\n    });\n  }\n}\n```\n\n## License\n\nThe MIT License (MIT)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimnew%2Fflutter_event_bus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimnew%2Fflutter_event_bus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimnew%2Fflutter_event_bus/lists"}