Debouncer class

A debouncer is a utility class that allows you to debounce a function call. It is useful to prevent a function from being called too frequently. For example, if you have a function that is called when the user types in a search box, you can use a debouncer to prevent the function from being called too frequently. This is useful to prevent the server from being overwhelmed by too many requests.

Example:

class SearchCubit extends Cubit<SearchState> {
  SearchCubit() : super(const SearchState());

  late final Debouncer _debouncer = Debouncer();

  void debouncedSearch(String searchQuery) {
    _debouncer.run(() => emit(state.copyWith(searchQuery: searchQuery.trim())));
  }

  @override
  Future<void> close() {
    _debouncer.dispose();
    return super.close();
  }

Constructors

Debouncer({Duration? delay})
Creates a new Debouncer with the given delay. If no delay is provided, the default delay of 400 milliseconds is used.

Properties

delay → Duration
The delay for the debouncer.
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

dispose() → void
Disposes the debouncer.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
run(VoidCallback action) → void
Runs the given action after the delay.
toString() → String
A string representation of this object.
inherited

Operators

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