Debounce class
A utility function to debounce a given function.
Debouncing ensures that the function is only called once after a specified duration has passed since the last time it was invoked. This is useful for scenarios where you want to limit the rate at which a function is executed, such as handling user input events or API calls.
Example:
final debouncer = Debounce(() => print('Hello, world!'));
By default, this will print 'Hello, world!' after 500 milliseconds. You can also specify a custom duration:
final debouncer = Debounce(() => print('Hello, world!'), duration: Duration(seconds: 1)});
To reset the debounce, and start the timer again, call the debounce
method with an optional new callback:
debouncer.debounce(newCallback: () => print('Later, world!'));
If this debounce is no longer needed, you can cancel it:
debouncer.cancel();
Constructors
- Debounce(void callback(), {Duration duration = _debounceDuration})
-
Constructs and starts the debouncer.
factory
- Debounce.stopped(void callback(), {Duration duration = _debounceDuration})
- Constructs debouncer but does not initialize the timer.
Properties
- callback → void Function()
-
Function called after Duration has elapsed.
final
- duration → Duration
-
Duration to wait for function to be ready to send.
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
-
cancel(
) → void - Cancels the debouncer.
-
debounce(
{void newCallback()?}) → void - Starts the debouncer.
-
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