Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DeBouncer mixin for Dart
import 'dart:async';
mixin DeBouncer {
Timer? _debounce;
deBouncer(Function callback) {
if (_debounce?.isActive ?? false) _debounce?.cancel();
_debounce = Timer(Duration(milliseconds: 300), () {
callback();
});
}
}
// Usage
class SearchController extends Controller with DeBouncer {
List<Product>? results;
void search(String query) async {
deBouncer(() async {
results?.clear();
results = await API.instance.searchProduct(query);
update();
});
}
}
//
//
//
//
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment