Flutter: search bar-send request to server after delay
1,058
Don't know if it's still useful, but I solved it using the Timer class.
Timer timer;
appBarTitle = new TextField(
controller: _filterController,
decoration: new InputDecoration(
hintText: 'Search...'
)
);
_filterController.addListener(() {
if (_filterController.text.isEmpty) {
} else {
if(timer != null){
timer.cancel();
timer = null;
}
timer = Timer(Duration(seconds: 1), userWrites);
}
});
userWrites(){
try {
print(_filterController.text);
//TODO: call
}catch(_) {
}
}
Comments
-
Little Monkey over 1 year
I have a search bar which does a rest call after 1 second that the user has typed something. But, in case the user digits more letters, like "asd", actually it will perform 3 requests, one for "a", one for "s" and one for "d". Is there a way to do only one call when the user stops?
The code is:
appBarTitle = new TextField( controller: _filterController, decoration: new InputDecoration( hintText: 'Search...' ) ); _filterController.addListener(() { if (_filterController.text.isEmpty) { } else { Future.delayed(Duration(seconds: 1), () { userWrites(_filterController.text); }); } }); userWrites(String filterName){ try { //TODO: call }catch(_) { } }
-
Little Monkey about 5 yearsSorry I haven't tested yet. I'm doing more important stuff right now. I'll let you know as soon as I have time.
-