A safe, async-aware debouncer for Dart & Flutter that prevents overlapping executions, timer leaks, and repeated boilerplate code.
safe_debouncer is a logic-only utility designed to handle one of the most common pain points in app development: debouncing actions correctly.
Most debounce implementations:
- leak
Timers - break on rebuilds
- don’t handle async callbacks
- allow overlapping executions
- get re-written differently in every project
safe_debouncer solves this once and for all.
- ✅ Supports sync & async callbacks
- ✅ Prevents overlapping executions
- ✅ Safe cancellation and disposal
- ✅ Rebuild-safe
- ✅ Key-based debouncing for multiple streams (
SafeDebouncerMap) - ✅ Individual debouncer instances (
SafeDebouncer) - ✅ Pure Dart (no Flutter dependency)
- ✅ Tiny, focused API
Add to your pubspec.yaml:
dependencies:
safe_debouncer: ^0.0.1import 'package:safe_debouncer/safe_debouncer.dart';
void main() {
final debouncer = SafeDebouncer(Duration(milliseconds: 500));
debouncer.call(() {
print('Debounced action executed!');
});
}import 'package:safe_debouncer/safe_debouncer.dart';
void main() async {
final debouncer = SafeDebouncer(Duration(milliseconds: 500));
await debouncer.callAsync(() async {
print('Debounced async action executed!');
});
}import 'package:safe_debouncer/safe_debouncer.dart';
void main() {
final debouncerMap = SafeDebouncerMap(Duration(milliseconds: 500));
debouncerMap.call('key1', () {
print('Debounced action executed for key1!');
});
debouncerMap.call('key2', () {
print('Debounced action executed for key2!');
});
}SafeDebouncer(Duration duration): Creates a newSafeDebouncerinstance with the specifiedduration.call(VoidCallback callback): Calls thecallbackafter the specifiedduration.callAsync(Future<void> Function() callback): Calls thecallbackafter the specifieddurationand returns aFuturethat completes when thecallbackis executed.
SafeDebouncerMap(Duration duration): Creates a newSafeDebouncerMapinstance with the specifiedduration.call(String key, VoidCallback callback): Calls thecallbackafter the specifieddurationfor the specifiedkey.callAsync(String key, Future<void> Function() callback): Calls thecallbackafter the specifieddurationfor the specifiedkeyand returns aFuturethat completes when thecallbackis executed.