enhanced_change_notifier
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.flutter-io.cn, 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? _token;
String? get token => _token;
set token(String? token) {
_token = token;
notifyListeners("token");
}
}
// GlobalFactory helps create a global singleton instance.
final GlobalFactory<AppModel> appStateModel = GlobalFactory(() => AppModel());
function _e_anyChangedListener() {
print("any property is changed");
}
function _e_tokenChangedListener(String property) {
print("$property is changed");
}
function _e_onceListener(String property) {
print("$property is changed, will notify only once.");
}
function _e_immediateListener(String property) {
print("$property is changed, will send immediately after listener is registered.");
}
appStateModel.getInstance().addListener(_e_anyChangedListener);
appStateModel.getInstance().addListener(_e_tokenChangedListener, target: 'token');
appStateModel.getInstance().addListener(_e_onceListener, target: 'token', once: true);
appStateModel.getInstance().addListener(_e_immediateListener, target: 'token', immediate: true);
A single implementation buffers pipelined listener pending a release signal.
import 'package:enhanced_change_notifier/signal.dart';
Signal isConsumerReady = Signal();
isConsumerReady.value = false;
// delayed signal release
Future.delayed(Duration(milliseconds: 300), () {
isConsumerReady.value = true;
});
// register listener consumed immediately or awaited once via Signal(True).
isConsumerReady.promise(() => print("Task 1 executed"));
isConsumerReady.promise(() => print("Task 2 executed"));
isConsumerReady.promise(() => print("Task 3 executed"));
Additional information
Feel free to file an issue if you have any problem.