Last active
November 28, 2019 04:22
-
-
Save ampersanda/dd780d0c8c1764c9567ede4935e522cc to your computer and use it in GitHub Desktop.
TextField Input Debouncing
This file contains 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
// declare variable at the top inside class | |
Timer _debounce; | |
Duration _debounceDuration = Duration(seconds: 1); | |
// initState | |
@override | |
void initState() { | |
super.initState(); | |
// add listener to textfield controller | |
_searchController.addListener(onSearchChanged); | |
} | |
// on change function | |
void onSearchChanged() { | |
if (_debounce?.isActive ?? false) _debounce.cancel(); | |
_debounce = Timer(_debounceDuration, () { | |
// TODO : DO SOMETHING | |
}); | |
} | |
@override | |
void dispose() { | |
controller.removeListener(listControllerCallback); | |
_searchController.removeListener(listControllerCallback); | |
_searchController.dispose(); | |
super.dispose(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment