Rest 颤振:搜索栏在延迟后向服务器发送请求
我有一个搜索栏,在用户键入内容1秒后执行rest调用。但是,如果用户输入更多的字母,如“asd”,实际上它将执行3个请求,一个为“a”,一个为“s”,一个为“d”。有没有办法在用户停止时只进行一次呼叫 代码是:Rest 颤振:搜索栏在延迟后向服务器发送请求,rest,search,flutter,searchbar,Rest,Search,Flutter,Searchbar,我有一个搜索栏,在用户键入内容1秒后执行rest调用。但是,如果用户输入更多的字母,如“asd”,实际上它将执行3个请求,一个为“a”,一个为“s”,一个为“d”。有没有办法在用户停止时只进行一次呼叫 代码是: appBarTitle = new TextField( controller: _filterController, decoration: new InputDecoration( hintText: 'Search...' ) ); _fil
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(_) {
}
}
不知道它是否仍然有用,但我使用Timer类解决了它
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(_) {
}
}
对不起,我还没有测试。我现在正在做更重要的事情。我一有时间就告诉你。