Debounce class
Creates a debounced function that delays invoking func
until after wait
milliseconds have elapsed since the last time the debounced function was
invoked. The debounced function comes with a Debounce.cancel method to cancel
delayed func
invocations and a Debounce.flush method to immediately invoke them.
Provide leading
and/or trailing
to indicate whether func
should be
invoked on the leading
and/or trailing
edge of the wait
interval.
The func
is invoked with the last arguments provided to the call
function. Subsequent calls to the debounced function return the result of
the last func
invocation.
Note: If leading
and trailing
options are true
, func
is
invoked on the trailing edge of the timeout only if the debounced function
is invoked more than once during the wait
timeout.
If wait
is Duration.zero and leading
is false
,
func
invocation is deferred until the next tick.
See David Corbacho's article for details over the differences between Debounce and Throttle.
Some examples:
Avoid calling costly network calls when user is typing something.
void fetchData(String query) async {
final data = api.getData(query);
doSomethingWithTheData(data);
}
final debouncedFetchData = Debounce(
fetchData,
const Duration(milliseconds: 350),
);
void onSearchQueryChanged(query) {
debouncedFetchData([query]);
}
Cancel the trailing debounced invocation.
void dispose() {
debounced.cancel();
}
Check for pending invocations.
final status = debounced.isPending ? "Pending..." : "Ready";
Constructors
Properties
Methods
-
call(
[List< Object?> ? args, Map<Symbol, Object?> ? namedArgs]) → Object? - Dynamically call this Debounce with the specified arguments.
-
cancel(
) → void - Cancels all the remaining delayed functions.
-
flush(
) → Object? - Immediately invokes all the remaining delayed functions.
-
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