-
-
Save adolfovj/f0f6ba6c9468a64c50b116c84c31c728 to your computer and use it in GitHub Desktop.
Flutter: Debouncer manual
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:async'; | |
| // Creditos | |
| // https://stackoverflow.com/a/52922130/7834829 | |
| class Debouncer<T> { | |
| Debouncer({ | |
| required this.duration, | |
| this.onValue | |
| }); | |
| final Duration duration; | |
| void Function(T value)? onValue; | |
| T? _value; | |
| Timer? _timer; | |
| T get value => _value!; | |
| set value(T val) { | |
| _value = val; | |
| _timer?.cancel(); | |
| _timer = Timer(duration, () => onValue!(_value!)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment