useDebounce function

void useDebounce(
  1. VoidCallback fn,
  2. Duration delay, [
  3. List<Object?>? keys
])

Flutter hook that debounces a function call.

Delays the execution of a function until after the specified delay has elapsed since the last time the function was scheduled to be called.

fn is the function to be debounced. delay is the duration to wait before executing the function. keys is an optional list of dependencies. The debounce timer resets whenever any of these dependencies change, similar to useEffect.

Example:

final searchController = TextEditingController();

useDebounce(() {
  // This will only execute 500ms after the user stops typing
  performSearch(searchController.text);
}, Duration(milliseconds: 500), [searchController.text]);

// Usage with a search field
TextField(
  controller: searchController,
  onChanged: (value) {
    // The search will be debounced automatically
  },
)

See also: useTimeoutFn for the underlying timeout implementation.

Implementation

void useDebounce(VoidCallback fn, Duration delay, [List<Object?>? keys]) {
  final timeout = useTimeoutFn(fn, delay);
  useEffect(() => timeout.reset, keys);
}