The `TypedEventNotifier` library allows notifying listeners with an object.

Overview

The TypedEventNotifier library allows notifying listeners with an object. listeners can be subscribed to only a special type or group of objects.

Installation

Add on pubspec.yml:

dependencies:
  typed_event_notifier: ... // latest package version

Usage

See example in /example folder

import 'package:typed_event_notifier/typed_event_notifier.dart';


/// Class [ExampleNotifier].
/// 
/// The example of notifier.
/// It can send notifications to listeners with an object
/// and notify listeners if they are registered for this object type
/// or extended objects.
class ExampleNotifier extends TypedEventNotifier<Event> {
  /// Create [ExampleNotifier] instance.
  ExampleNotifier();
  
  /// Will notify listeners with [CurrentPageChangedEvent] event.
  void currentPage(int index) {
    _currentPage = index;
    notifyListeners(CurrentPageChangedEvent(currentPage: currentPage));
  }

  /// Will notify listeners with [PagesLoadedEvent] event.
  set loadedPages(Set<int> set) {
    _loadedPages.addAll(set);
    notifyListeners(PagesLoadedEvent(pages: set));
  }
}


//The part of example of listener on `current page changed` event only.
class _CurrentPageOnlyListenerState extends State<CurrentPageOnlyListener> {
  String message = 'CurrentPageOnly: empty';

  // Will receive events only with CurrentPageChangedEvent type.
  void currentPageChanged(CurrentPageChangedEvent event) {
    setState(() {
      message = 'CurrentPageOnly: now current page is ${event.currentPage}';
    });
  }

  @override
  void initState() {
    widget.notifier.addListener(currentPageChanged);
    super.initState();
  }

  @override
  void dispose() {
    widget.notifier.removeListener(currentPageChanged);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Text(message);
  }

}

// The part of example of listener on any event.
class _AnyListenerState extends State<AnyListener> {
  String message = 'Any: empty';

  // Will receive events with CurrentPageChangedEvent and PagesLoadedEvent type.
  void any(Event event) {
    if (event is CurrentPageChangedEvent) {
      setState(() {
        message = 'Any: now current page is ${event.currentPage}';
      });
    }
    if (event is PagesLoadedEvent) {
      setState(() {
        message = 'Any: new loaded pages is ${event.pages}';
      });
    }
  }

  @override
  void initState() {
    widget.notifier.addListener(any);
    super.initState();
  }

  @override
  void dispose() {
    widget.notifier.removeListener(any);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Text(message);
  }
}

// The events for example, which will be sent through the notifier.
// They have abstract base class (used as parent type),
// and extends from it events.
// for example two types with different content.
/// Class [Event]. 
abstract class Event {
  /// Create [Event] instance.
  Event();
}

/// Class [CurrentPageChangedEvent].
class CurrentPageChangedEvent extends Event {
  /// Index of current page.
  final int currentPage;

  /// Create [CurrentPageChangedEvent] instance.
  CurrentPageChangedEvent({
    required this.currentPage,
  }) : super();
}

/// Class [PagesLoadedEvent].
class PagesLoadedEvent extends Event {
  /// Indexes of loaded pages.
  final Set<int> pages;

  /// Create [PagesLoadedEvent] instance.
  PagesLoadedEvent({
    required this.pages,
  }) : super();
}
You might also like...

A Dart library to parse Portable Executable (PE) format

pefile A Dart library to parse Portable Executable (PE) format Usage A simple usage example: var pe = pefile.parse('C:\\Windows\\System32\\notepad.exe

Sep 12, 2022

This library contains methods that make it easy to consume Mpesa Api.

This library contains methods that make it easy to consume Mpesa Api. It's multi-platform, and supports CLI, server, mobile, desktop, and the browser.

Dec 15, 2021

Scribble is a lightweight library for freehand drawing in Flutter supporting pressure, variable line width and more!

Scribble is a lightweight library for freehand drawing in Flutter supporting pressure, variable line width and more!

Scribble Scribble is a lightweight library for freehand drawing in Flutter supporting pressure, variable line width and more! A

Dec 16, 2022

An alternative random library for Dart.

Randt Randt library for Dart... Description Use Randt to get a random integer from a list, generate random integer in a specific range and generate ra

Nov 21, 2021

A library for YAML manipulation with comment and whitespace preservation.

Yaml Editor A library for YAML manipulation while preserving comments. Usage A simple usage example: import 'package:yaml_edit/yaml_edit.dart'; void

Dec 26, 2022

The Dart Time Machine is a date and time library for Flutter, Web, and Server with support for timezones, calendars, cultures, formatting and parsing.

The Dart Time Machine is a date and time library for Flutter, Web, and Server with support for timezones, calendars, cultures, formatting and parsing.

The Dart Time Machine is a date and time library for Flutter, Web, and Server with support for timezones, calendars, cultures, formatting and parsing.

Oct 8, 2021

A comprehensive, cross-platform path manipulation library for Dart.

A comprehensive, cross-platform path manipulation library for Dart. The path package provides common operations for manipulating paths: joining, split

Dec 29, 2022

Boilerplate-free form validation library

Valform Boilerplate-free form validation library. Preface Why? Why not Formz? Why Valform? Getting started Simple Usage Inspiration Why? There is no c

Nov 14, 2021

Boilerplate-free form validation library

Trigger Boilerplate-free form validation library. Preface Why? Why not Formz? Why Trigger? Getting started Simple Usage Inspiration Why? There is no c

Nov 14, 2021
Owner
Evgeniy Ilyin
Evgeniy Ilyin
Provides null-safety implementation to simplify JSON data handling by adding extension method to JSON object

Lazy JSON Provides null-safety implementation to simplify JSON data handling by adding extension method to JSON object and JSON array. Getting started

Kinnara Digital Studio 0 Oct 27, 2021
Reflectable is a Dart library that allows programmers to eliminate certain usages of dynamic reflection by specialization of reflective code to an equivalent implementation using only static techniques

Reflectable is a Dart library that allows programmers to eliminate certain usages of dynamic reflection by specialization of reflective code to an equivalent implementation using only static techniques. The use of dynamic reflection is constrained in order to ensure that the specialized code can be generated and will have a reasonable size.

Google 318 Dec 31, 2022
An auto mapper for Dart. It allows mapping objects of different classes automatically and manually using JSON serialization.

AutoMapper for Dart An auto mapper for Dart. It allows mapping objects of different classes automatically and manually using JSON serialization. Examp

Leynier Gutiérrez González 7 Aug 24, 2022
Allows you to cancel compute operation.

cancelable-compute.dart Spawn an isolate, run callback on that isolate, passing it message, and return the value returned by callback or canceled by u

Olzhas Suleimen 8 Oct 7, 2022
Dependency Injection is a great design pattern that allows us to eliminate rigid dependencies between elements and it makes the application more flexible

GetX lib DI pattern Dependency Injection is a great design pattern that allows us to eliminate rigid dependencies between elements and it makes the ap

Trương Việt Hoàng 4 Feb 1, 2022
Extension functions on ValueListenable that allows you to work with them almost as if it was a synchronous stream.

functional_listener Extension functions on ValueListenable that allows you to work with them almost as if it was a synchronous stream. Each extension

null 54 Oct 9, 2022
A flutter package allows you to search all universities in Brazil

Universities This package allows you to search all universities in Brazil. Usage To use this plugin, add universities as a dependency in your pubspec.

Gabriel Patrick Souza 2 Oct 24, 2021
A flutter package that allows you to transform your excel to json

excel_to_json A package that allows you to transform your excel to the following format: Excel To JSON Getting Started At current the package allows y

Vitor Amaral de Melo 0 Nov 7, 2022
This package allows programmers to annotate Dart objects in order to Serialize / Deserialize them to / from JSON

This package allows programmers to annotate Dart objects in order to Serialize / Deserialize them to / from JSON. Why? Compatible with all target plat

Alexander Mazuruk 356 Jan 6, 2023
Fluro is a Flutter routing library that adds flexible routing options like wildcards, named parameters and clear route definitions.

Fluro is a Flutter routing library that adds flexible routing options like wildcards, named parameters and clear route definitions.

Luke Pighetti 3.5k Jan 4, 2023