Throttle class
Creates a throttled function that only invokes func
at most once per
every wait
milliseconds. The throttled function comes with a Throttle.cancel
method to cancel delayed func
invocations and a Throttle.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
timeout.
The func
is invoked with the last arguments provided to the
throttled function. Subsequent calls to the throttled 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 throttled 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 Throttle and Debounce.
Some examples:
Avoid excessively rebuilding UI progress while uploading data to server.
void updateUI(Data data) {
updateProgress(data);
}
final throttledUpdateUI = Throttle(
updateUI,
const Duration(milliseconds: 350),
);
void onUploadProgressChanged(progress) {
throttledUpdateUI(progress);
}
Cancel the trailing throttled invocation.
void dispose() {
throttled.cancel();
}
Check for pending invocations.
final status = throttled.isPending ? "Pending..." : "Ready";
Constructors
Properties
Methods
-
call(
[List< Object> ? args, Map<Symbol, Object> ? namedArgs]) → Object? - Dynamically call this Throttle 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