enhanced_change_notifier

pub package pub points GitHub Issues GitHub Forks GitHub Stars GitHub License

Support for targeted notifications on object property changes.

Enhanced ChangeNotifiers introduce three new features in addition to all existing ChangeNotifier capabilities in Flutter Core:

  • target: notifies listeners at the moment a specified property changes.
  • once: notifies listeners only once at the moment of a change.
  • immediate: allows notifications to be sent immediately after a listener is registered and upon subsequent changes.

Platform Support

Android iOS MacOS Web Linux Windows

Requirements

  • Flutter >=3.0.0 <4.0.0
  • Dart >=2.17.0

Getting started

published on pub.dev, run this Flutter command

flutter pub add enhanced_change_notifier

Usage in Dart

Multiple properties or Mutable data types, extending EnhancedChangeNotifier directly to meet flexible requirements like cache or targeted listener.

import 'package:enhanced_change_notifier/enhanced_change_notifier.dart';

class AppModel extends EnhancedChangeNotifier {

  String? get token => super.properties["token"];
  set token(String? token) {
    super.properties["token"] = token;
  }
}

// GlobalFactory helps create a global singleton instance.
final GlobalFactory<AppModel> appStateModel = GlobalFactory(() => AppModel());

function _e_anyChangedListener() {
  print("any property is changed");
}

// Register all types of listeners
appStateModel.getInstance().addListener(() => print("any property is changed"));
appStateModel.getInstance().addListener((String property) => print("$property is changed"), target: 'token');
appStateModel.getInstance().addListener((String property) => print("$property is changed, will notify only once."), target: 'token', once: true);
appStateModel.getInstance().addListener((String property) => print("$property is changed, will send immediately after listener is registered."), target: 'token', immediate: true);

// Remove a specific listener
appStateModel.getInstance().removeListener(_e_anyChangedListener);

Signal A single implementation buffers pipelined listener pending a release signal.


import 'package:enhanced_change_notifier/signal.dart';

Signal isConsumerReady = Signal();
isConsumerReady.value = false;

// Register listener consumed immediately or awaited once via Signal(True).
isConsumerReady.promise(() => print("Task 1 executed"));
isConsumerReady.promise(() => print("Task 2 executed"));

// Release delayed signal
Future.delayed(Duration(milliseconds: 300), () => isConsumerReady.value = true);

EnhancedLatchNotifier supports complex data types for delayed notifications, unlike Signal which is limited to boolean values

final GlobalFactory<DelayedLatch> latchDemo = GlobalFactory(() => DelayedLatch());
latchDemo.getInstance().addListener((event) {
  if (event.actionId == "test") {
    print("triggered ==> ${event.actionId}, ${event.value}");
  }
});
// Fire latch and cache state event
latchDemo.getInstance().fire(LatchStateEvent("test", 99));
// Listener triggered when unlatch called
Future.delayed(const Duration(seconds: 3), () => latchDemo.getInstance().unlatch());

Additional information

Feel free to file an issue if you have any problem.