An Event-based system, highly inspired by NodeJS's Event Emitter

Overview

Pub.dev package GitHub repository

Event Emitter

An Event-based system, highly inspired by NodeJS's Event Emitter. This implementation uses generic types to allow for multiple data types, while still being intuitive.

Based on JavaScript and suitable for Dart and Flutter with type safety.

Features

  • Attach multiple listeners to an event.
  • Listen to a topic and data type.
  • Emit a message on a specific topic to be broadcasted to all listeners.
  • Type safety.
  • Use callbacks with EventEmitter.
  • Use streams with EventStreamEmitter.
  • Can be extended to create custom event emitter objects.

Getting started

Install it using pub:

dart pub add events_emitter

And import the package:

import 'package:events_emitter/events_emitter.dart';

Usage

final events = EventEmitter();

events.on('message', (String data) => print('String: $data'));
events.on('message', (int data) => print('Integer: $data'));

events.emit('message', 'Hello World');
events.emit('message', 42);

// [Output]
// String: Hello World
// Integer: 42

To remove a specific listener, you can use the subscription to stop it.

final listener = events.on('message', ... ));
listener.cancel();

Remove listeners, by targeting a type, topic and callback.

// Remove all listeners
events.off();

// Remove listeners of type `String`
events.off<String>();

// Remove listeners on topic `message`
events.off(topic: 'message');

// Remove listeners of type `String` on topic `message`
events.off<String>(topic: 'message');

Why is this package different?

events_emitter implements the Event-based system using streams, then wraps it into a class that manages the callbacks, to make it more like what people are used to see in an EventEmitter. This allows you also to use the streams instead of callbacks if needed.

And something very important, events_emitter allows you to use type-safe events, so you can use the same topic for different data types. Not having to worry about the wrong type being passed in.

Example

The EventEmitter class can be used by itself or can be extended to create a custom event emitter.

import 'package:events_emitter/events_emitter.dart';

class Person extends EventEmitter {
    String name;

    Person(this.name);

    void say(String message) => emit('say', message);
    void eat(String food) => emit('eat', food);
    void jump(double height) => emit('jump', height);
}

void main() {
  final person = Person('John');

  person.on('say', (String message) => print('${person.name} said: $message'));
  person.on('eat', (String food) => print('${person.name} ate $food'));
  person.on('jump', (double height) => print('${person.name} jumped $height meters'));

  person.say('I\'m a human!');
  person.eat('apple');
  person.jump(0.5);

  // [Output]
  // John said: I'm a human!
  // John ate apple
  // John jumped 0.5 meters
}

More examples:

You might also like...

A highly customizable Flutter color picker.

A highly customizable Flutter color picker.

FlexColorPicker FlexColorPicker is a customizable color picker for Flutter. The ColorPicker can show six different types of color pickers, three of wh

Dec 14, 2022

⚡️A highly customizable, powerful and easy-to-use alerting library for Flutter.

⚡️A highly customizable, powerful and easy-to-use alerting library for Flutter.

Flash ⚡️ A highly customizable, powerful and easy-to-use alerting library for Flutter. Website: https://sososdk.github.io/flash Specs This library all

Jan 5, 2023

Clock loader - Highly versatile Widget display the smooth and creative loader named as clock loader

Clock loader - Highly versatile Widget display the smooth and creative loader named as clock loader

Clock Loader Highly versatile Widget display the smooth and creative loader name

Dec 30, 2022

A Flutter slidable widget that provides an easy to use configuration. Highly customisable. Just as you want it!

A Flutter slidable widget that provides an easy to use configuration. Highly customisable. Just as you want it!

A Flutter slidable widget that provides an easy to use configuration. Highly customisable. Just as you want it!

Dec 8, 2022

A package that provides a highly customizable sheet widget that snaps to different vertical & horizontal positions

A package that provides a highly customizable sheet widget that snaps to different vertical & horizontal positions

Snapping Sheet A package that provides a highly customizable sheet widget that snaps to different vertical & horizontal positions Can adapt to scrolla

Dec 6, 2022

A highly customizable Flutter widget to render and interact with JSON objects.

A highly customizable Flutter widget to render and interact with JSON objects.

The spreadsheet with superpowers ✨ ! JSON Data Explorer A highly customizable widget to render and interact with JSON objects. Features Expand and col

Dec 21, 2022

Code Snippets of highly interactive Flutter Templates that can be implemented in a Native Flutter App.

Code Snippets of highly interactive Flutter Templates that can be implemented in a Native Flutter App.

Native Frontend Flutter About the Repository We are on a mission to make things easy and convenient for all the users who just want to save their time

Sep 5, 2022

Add beautiful animated effects & builders in Flutter, via an easy, highly customizable unified API.

Add beautiful animated effects & builders in Flutter, via an easy, highly customizable unified API.

Flutter Animate A performant library that makes it simple to add almost any kind of animated effect in Flutter. Pre-built effects, like fade, scale, s

Dec 25, 2022

The flutter_calendar_widget is highly customizable calendar widget.

The flutter_calendar_widget is highly customizable calendar widget.

flutter_calendar_widget The flutter_calendar_widget is highly customizable calendar widget. Not only can you change the style, but you can also change

Jun 24, 2022
Comments
  • Make package only dart dependant

    Make package only dart dependant

    Pub.dev currently shows that Flutter is required for this package to function properly. Removing this line will let the package manager know that this package can work just fine without Flutter.

    https://imgur.com/a/mWlqu8t

    Great work by the way, this is great.

    opened by XanderNuytinck 0
Owner
JUST A SNIPER ツ
JUST A SNIPER ツ
A grid-based layout system for Flutter, inspired by CSS Grid Layout

Flutter Layout Grid A powerful grid layout system for Flutter, optimized for complex user interface design. Click images to see their code ✨ Featuring

Felt 307 Dec 24, 2022
Flutter UI library based on IBM's Carbon Design System 💎

Flutter Carbon ‌Carbon is IBM’s open-source design system for products and experiences. With the IBM Design Language as its foundation, the system con

Nour El-Din Shobier 89 Jan 5, 2023
CartToGo is an IoT-based smart system that is connected to an iOS mobile application

CartToGo is an IoT-based smart system that is connected to an iOS mobile application, used by supermarket shoppers to reduce their waiting time in the queue lines and that’s done by enabling them to scan each product’s barcode to display its price and total price on the LCD display as well as the iOS mobile application.

CartToGo 5 Dec 15, 2022
Build a system that recommends jobs based on resume.

Job Findr Problem Statement: Build a system that recommends jobs based on resume. Problem Description: The problem statement suggests to build a syste

null 2 Jul 8, 2022
Create highly customizable, simple, and controllable autocomplete fields for Flutter.

Field Suggestion Installing Depend on it Add this to your package's pubspec.yaml file: dependencies: field_suggestion: <latest_version> Install it Y

Ismael Shakverdiev 21 Oct 18, 2022
Powerful, helpfull, extensible and highly customizable API's that wrap http client to make communication easier with Axelor server with boilerplate code free.

flutter_axelor_sdk Powerful, helpful, extensible and highly customizable API's that wrap http client to make communication easier with Axelor server w

Abd al-Rahman al-Ktefane 5 Dec 25, 2022
Custom dropdown widget allows to add highly customizable widget in your projects with proper open and close animations and also comes with form required validation.

Custom Dropdown Custom Dropdown package lets you add customizable animated dropdown widget. Features Lots of properties to use and customize dropdown

Abdullah Chauhan 22 Dec 29, 2022
A highly customizable Flutter color picker.

FlexColorPicker FlexColorPicker is a customizable color picker for Flutter. The ColorPicker can show six different types of color pickers, three of wh

Rydmike 132 Dec 14, 2022
Speed Share is a highly available file sharing terminal on LAN(local area network) developed by flutter framework.

速享 Language: 中文简体 | English 这是一款完全基于局域网的文件互传终端,速享不使用任何服务器,不使用您的移动流量,不收集任何用户数据,完全的点对点传输。 可以快速共享文本消息,图片或其他文件,文件夹。 适用于局域网中的文件互传,解决 QQ,微信等上传文件会经过服务器的问题,或者

null 477 Dec 31, 2022
A highly customisable and simple widget for having iOS 13 style tab bars.

cupertino_tabbar A highly customisable and simple widget for having iOS 13 style tab bars. It is highly recommended to read the documentation and run

AliYigitBireroglu 98 Oct 31, 2022