Debouncer<T> class

A utility class to debounce function calls.

It delays the execution of a function until a specified duration has passed since the last call. This ensures that only the final call in a rapid sequence is executed.

Commonly used in text input fields or live search scenarios, where you want to wait until the user has stopped typing before performing an action (like firing an API request).


Example:

final debouncer = Debouncer<String>(Duration(milliseconds: 300));

debouncer.action = (searchTerm) async {
  // Simulates an API call delay
  final results = await fakeApiSearch(searchTerm);
  print('Results for "$searchTerm": $results');
};

// Mock API function that returns results after 1 second
Future<List<String>> fakeApiSearch(String query) async {
  await Future.delayed(const Duration(seconds: 1));
  return List.generate(3, (index) => '$query result $index');
}

// Simulated rapid calls (user typing)
debouncer.call('Fl');
debouncer.call('Flu');
debouncer.call('Flutt');
debouncer.call('Flutter'); // Only this will trigger after delay

Notes:

  • Only the last call will be executed after the delay.
  • If a new call is made before the delay ends, the timer resets.
  • Useful for optimizing performance and avoiding unnecessary processing.

Constructors

Debouncer(Duration delay)

Properties

action ↔ void Function(T value)?
getter/setter pair
delay Duration
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

call(T value) → void
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited