notifier_plus 0.1.1
notifier_plus: ^0.1.1 copied to clipboard
Notifier Plus is a Flutter plugin that improves reactive data handling, merges consecutive state updates, and prevents redundant rebuilds. It provides specialized notifiers (both synchronous and async [...]
notifier_plus #
notifier_plus is a high-performance Flutter state management library designed to optimize Listenable updates.
Created by tachibana-shin, this library eliminates unnecessary UI rebuilds caused by ValueNotifier and Listenable.merge.
📖 Table of Contents #
- Why notifier_plus?
- Installation
- Core Classes
- Watch Widgets
- Utilities
- Comparison Table
- Contributing
- License
📌 Why notifier_plus? #
🚀 The Problem with ValueNotifier and Listenable.merge #
-
ValueNotifiercauses redundant UI rebuilds when updated consecutively:a.value++; a.value++; // Causes two rebuilds, but only one is needed -
Listenable.mergeinvokes multiple rebuilds when multiple dependencies update:a.value++; b.value++; // Triggers two rebuilds instead of one
✅ Solution: notifier_plus #
- Batches multiple updates within the same frame to trigger only one rebuild.
- Efficient state management that reduces unnecessary UI updates.
- Easy to use with
ValueNotifier-like API but optimized for performance.
📦 Installation #
Add to pubspec.yaml:
dependencies:
notifier_plus:
git:
url: https://github.com/tachibana-shin/notifier_plus.git
Import it:
import 'package:notifier_plus/notifier_plus.dart';
🟢 Core Classes #
Notifier<T> - Optimized ValueNotifier #
A ChangeNotifier implementation that ensures updates are only triggered once per frame.
final counter = Notifier<int>(0);
counter.addListener(() {
print("Counter updated: ${counter.value}");
});
counter.value++; // Only one listener call per frame
✅ Features:
- Prevents redundant updates.
- Implements
ValueListenable<T>for seamless integration.
ComputedNotifier<T> - Derived State Management #
Computes a value based on other Notifier dependencies.
final a = Notifier<int>(1);
final b = Notifier<int>(2);
final sum = ComputedNotifier(() => a.value + b.value, depends: [a, b]);
sum.addListener(() {
print("Sum updated: ${sum.value}");
});
✅ Features:
- Automatically tracks dependencies.
- Only updates when dependent values change.
ComputedAsyncNotifier<T> - Async Computed State #
Handles derived state that depends on asynchronous computations.
final asyncSum = ComputedAsyncNotifier<int>(
() async => await fetchData(),
depends: [a, b]
);
✅ Features:
- Supports async state calculations.
- Notifies listeners only when computation completes.
🏗 Watch Widgets #
Efficient UI rebuilding for state changes.
WatchComputed<T> - Watches a Single ComputedNotifier #
WatchComputed<int>(
computed: sum,
builder: (context, value) {
return Text("Sum: $value");
},
);
WatchAsyncComputed<T> - Watches an Async Computed Value #
WatchAsyncComputed<int>(
computed: asyncSum,
builder: (context, value) {
return value == null ? CircularProgressIndicator() : Text("Sum: $value");
},
);
WatchComputes<T> - Watches Multiple ComputedNotifiers #
WatchComputes<int>(
computes: [a, b],
builder: (context) {
return Text("A: ${a.value}, B: ${b.value}");
},
);
WatchNotifier<T> - Watches Multiple ChangeNotifiers #
WatchNotifier(
depends: [a, b],
builder: (context) {
return Text("State changed: A=${a.value}, B=${b.value}");
},
);
⚡ Utilities #
oneCallTask(VoidCallback callback) #
A helper function to debounce state updates within the same frame.
final debouncedUpdate = oneCallTask(() {
print("Updated!");
});
debouncedUpdate(); // Calls once per frame
watch(List<Listenable> depends, VoidCallback callback, {bool immediate}) #
Registers a callback that listens to multiple Listenables efficiently.
watch([a, b], () {
print("A or B changed");
});
📌 Comparison Table #
| Feature | notifier_plus |
|---|---|
| Prevents redundant UI rebuilds | ✅ |
Optimized ValueNotifier replacement |
✅ |
| Efficient dependency tracking | ✅ |
| Supports async computed values | ✅ |
| Lightweight & easy to use | ✅ |
🛠 Contributing #
Pull requests are welcome! Feel free to submit issues or feature requests.
📜 License #
MIT License. See LICENSE for details.