Skip to content

Instantly share code, notes, and snippets.

@ampersanda
Last active November 28, 2019 04:22
Show Gist options
  • Save ampersanda/dd780d0c8c1764c9567ede4935e522cc to your computer and use it in GitHub Desktop.
Save ampersanda/dd780d0c8c1764c9567ede4935e522cc to your computer and use it in GitHub Desktop.
TextField Input Debouncing
// 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