debounceWith<T> static method

dynamic Function(T) debounceWith<T>(
  1. void func(
    1. T
    ), {
  2. Duration delay = const Duration(milliseconds: 300),
})

Generic debounce function that passes value to callback

Example:

final debouncedSave = FSUtils.debounceWith<String>((value) => save(value));
textField.onChanged = debouncedSave;

Implementation

static Function(T) debounceWith<T>(
  void Function(T) func, {
  Duration delay = const Duration(milliseconds: 300),
}) {
  Timer? timer;
  return (T value) {
    timer?.cancel();
    timer = Timer(delay, () => func(value));
  };
}