debounce method Null safety

Future debounce(
  1. Function func
)

allows you to control events being triggered successively and, if the interval between two sequential occurrences is less than a certain amount of time (e.g. one second), it completely ignores the first one.

Implementation

Future<dynamic> debounce(Function func) async {
  if (_waiter?.isActive ?? false) {
    _waiter?.cancel();
    _resultSC.sink.add(null);
  }
  _isReady = false;
  _stateSC.sink.add(false);
  _waiter = Timer(_duration, () {
    _isReady = true;
    _stateSC.sink.add(true);
    _resultSC.sink.add(Function.apply(func, []));
  });
  return _resultSC.stream.first;
}