Debounce class

Debounce is used to control the frequency of function calls with support for flexible delay and "leading" / "trailing" options.

This class is especially useful when you need to prevent frequent repeated calls:

  • when entering text (for example, search)
  • when processing scroll events
  • when using resize handlers
  • when pressing buttons (for example, double-click protection)

Supports:

  • delay — How long to wait after the last call before executing
  • leading — If true, callback will be called on the first call before the interval expires.
  • trailing — If true, callback will be called after the interval ends
  • leading && trailing If both are true, leading callback will be called immediately before the interval expires and trailing callback will be called after the interval ends (if there were repeated calls)

Usage example:

final _debounce = Debounce(delay: Duration(seconds: 1), leading: false, trailing: true); ✅ Good!
Config: leading: false, trailing: true
Input:  1-2-3---4---5-6-|
Output: ------3---4-----6|

final _debounce = Debounce(delay: Duration(seconds: 1), leading: true, trailing: false); ✅ Good!
Config: leading: true, trailing: false
Input:  1-2-3---4---5-6-|
Output: 1-------4---5---|

final _debounce = Debounce(delay: Duration(seconds: 1), leading: true, trailing: true); ✅ Good!
Config: leading: true, trailing: true
Input:  1-2-3---4---5-6-|
Output: 1-----3-4---5---6|

final _debounce = Debounce(delay: Duration(seconds: 1), leading: false, trailing: false); ❌ Bad! Output empty!
Config: leading: false, trailing: false
Input:  1-2-3---4---5-6-|
Output: ----------------|
final _debounce = Debounce(delay: Duration(milliseconds: 500));

void onTextChanged(String text) {
  _debounce(() {
    // ...
  });
}

@override
void dispose() {
  _debounce.dispose();
  super.dispose();
}

Constructors

Debounce.new({Duration delay = defaultDelay, bool leading = false, bool trailing = true})
Debounce is used to control the frequency of function calls with support for flexible delay and "leading" / "trailing" options.

Properties

delay Duration
User-defined delay between function calls.
final
hashCode int
The hash code for this object.
no setterinherited
isActive bool
  • isActive — Returns true if the instance is active (has not been destroyed).
  • no setter
    isNotActive bool
  • isNotActive — Returns true if the instance was destroyed by calling dispose or both disabled: leading and trailing (both false).
  • no setter
    isTimerActive bool
  • isTimerActive — Returns true if the internal timer is currently active.
  • no setter
    leading bool
    If `true', callback is called immediately on the first call to call.
    final
    runtimeType Type
    A representation of the runtime type of the object.
    no setterinherited
    trailing bool
    If `true', callback is called once at the end of the delay.
    final

    Methods

    call(VoidCallback callback) → void
    Calls the callback, taking into account the leading and trailing settings.
    dispose() → void
    Destroys the Debounce instance and clears all internal data.
    flush() → void
    Immediately performs a delayed callback if the timer is still active.
    noSuchMethod(Invocation invocation) → dynamic
    Invoked when a nonexistent method or property is accessed.
    inherited
    reset() → void
    Resets the current timer and clears the callback.
    toString() String
    A string representation of this object.
    inherited

    Operators

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

    Constants

    defaultDelay → const Duration
    The default delay is 800 milliseconds.