debounce static method

VoidCallback debounce(
  1. VoidCallback func, {
  2. Duration delay = const Duration(milliseconds: 300),
})

Debounce function for limiting frequent function calls

Delays function execution until after wait time has elapsed since last call. Useful for search inputs, resize events, etc.

Example:

final debouncedSearch = FSUtils.debounce(() => performSearch());
searchController.addListener(debouncedSearch);

Implementation

static VoidCallback debounce(
  VoidCallback func, {
  Duration delay = const Duration(milliseconds: 300),
}) {
  Timer? timer;
  return () {
    timer?.cancel();
    timer = Timer(delay, func);
  };
}