如何限制 Dart 2 中的 TextEditingController 侦听器事件 - Flutter
How to throttle TextEditingController listener events in Dart 2 - Flutter
我想知道是否有一些我可能错过的内置功能。我试图找到类似的东西,但我找到的唯一包 (throttle) 不再支持 Dart 2
这是我想要限制的代码部分
final TextEditingController _filter = new TextEditingController();
String _searchText = "";
_filter.addListener(() {
if (_filter.text.isEmpty) {
setState(() {
_searchText = "";
});
} else {
setState(() {
_searchText = _filter.text;
});
}
//This action is being fired TOO many times :(
widget.onUpdateSearchTerm(_searchText);
});
有什么想法吗?
我会使用 throttle
或 debounce
来自 rxdart
在 rxdart 0.22.x 使用 Observable
final TextEditingController _filter = new TextEditingController();
String _searchText = "";
final _textUpdates = StreamController<String>();
_filter.addListener(() => _textUpdates.add(_filter.text));
Observable(_textUpdates.stream)
.throttle(const Duration(milliseconds: 700))
.forEach((s) {
if (s.isEmpty) {
setState(() {
_searchText = "";
});
} else {
setState(() {
_searchText = s;
});
}
//This action is being fired TOO many times :(
widget.onUpdateSearchTerm(_searchText);
});
在 rxdart 0.23.x 及以后
final TextEditingController _filter = new TextEditingController();
String _searchText = "";
final _textUpdates = StreamController<String>();
_filter.addListener(() => _textUpdates.add(_filter.text));
_textUpdates.stream
.throttle(const Duration(milliseconds: 700))
.forEach((s) {
if (s.isEmpty) {
setState(() {
_searchText = "";
});
} else {
setState(() {
_searchText = s;
});
}
//This action is being fired TOO many times :(
widget.onUpdateSearchTerm(_searchText);
});
另见
我想知道是否有一些我可能错过的内置功能。我试图找到类似的东西,但我找到的唯一包 (throttle) 不再支持 Dart 2
这是我想要限制的代码部分
final TextEditingController _filter = new TextEditingController();
String _searchText = "";
_filter.addListener(() {
if (_filter.text.isEmpty) {
setState(() {
_searchText = "";
});
} else {
setState(() {
_searchText = _filter.text;
});
}
//This action is being fired TOO many times :(
widget.onUpdateSearchTerm(_searchText);
});
有什么想法吗?
我会使用 throttle
或 debounce
来自 rxdart
在 rxdart 0.22.x 使用 Observable
final TextEditingController _filter = new TextEditingController();
String _searchText = "";
final _textUpdates = StreamController<String>();
_filter.addListener(() => _textUpdates.add(_filter.text));
Observable(_textUpdates.stream)
.throttle(const Duration(milliseconds: 700))
.forEach((s) {
if (s.isEmpty) {
setState(() {
_searchText = "";
});
} else {
setState(() {
_searchText = s;
});
}
//This action is being fired TOO many times :(
widget.onUpdateSearchTerm(_searchText);
});
在 rxdart 0.23.x 及以后
final TextEditingController _filter = new TextEditingController();
String _searchText = "";
final _textUpdates = StreamController<String>();
_filter.addListener(() => _textUpdates.add(_filter.text));
_textUpdates.stream
.throttle(const Duration(milliseconds: 700))
.forEach((s) {
if (s.isEmpty) {
setState(() {
_searchText = "";
});
} else {
setState(() {
_searchText = s;
});
}
//This action is being fired TOO many times :(
widget.onUpdateSearchTerm(_searchText);
});
另见