Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions packages/material_ui/lib/src/search_anchor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,8 @@ class _ViewContentState extends State<_ViewContent> {
Iterable<Widget> result = <Widget>[];
String? searchValue;
Timer? _timer;
// Identifies the latest call so that older async results cannot replace newer ones.
int _suggestionsCallId = 0;

@override
void initState() {
Expand Down Expand Up @@ -1008,14 +1010,9 @@ class _ViewContentState extends State<_ViewContent> {
_timer?.cancel();
_timer = Timer(Duration.zero, () async {
searchValue = _controller.text;
final Iterable<Widget> suggestions = await widget.suggestionsBuilder(context, _controller);
await _buildSuggestions();
_timer?.cancel();
_timer = null;
if (mounted) {
setState(() {
result = suggestions;
});
}
});
}
}
Expand Down Expand Up @@ -1056,15 +1053,21 @@ class _ViewContentState extends State<_ViewContent> {
Future<void> updateSuggestions() async {
if (searchValue != _controller.text) {
searchValue = _controller.text;
final Iterable<Widget> suggestions = await widget.suggestionsBuilder(context, _controller);
if (mounted) {
setState(() {
result = suggestions;
});
}
await _buildSuggestions();
}
}

Future<void> _buildSuggestions() async {
final int callId = ++_suggestionsCallId;
final Iterable<Widget> suggestions = await widget.suggestionsBuilder(context, _controller);
if (!mounted || callId != _suggestionsCallId) {
return;
}
setState(() {
result = suggestions;
});
}

@override
Widget build(BuildContext context) {
final Widget defaultLeading = BackButton(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
changelog: |
- Prevents stale asynchronous suggestions from replacing newer `SearchAnchor` results.
version: patch
40 changes: 40 additions & 0 deletions packages/material_ui/test/search_anchor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2261,6 +2261,46 @@ void main() {
expect(controller.value.text, suggestion);
});

testWidgets('SearchAnchor ignores out-of-order async suggestions', (WidgetTester tester) async {
final requests = <String, Completer<Iterable<Widget>>>{};

await tester.pumpWidget(
MaterialApp(
home: SearchAnchor(
builder: (BuildContext context, SearchController controller) {
return const Icon(Icons.search);
},
suggestionsBuilder: (BuildContext context, SearchController controller) {
final String query = controller.text;
if (query.isEmpty) {
return <Widget>[];
}
final request = Completer<Iterable<Widget>>();
requests[query] = request;
return request.future;
},
),
),
);

await tester.tap(find.byIcon(Icons.search));
await tester.pumpAndSettle();

await tester.enterText(findTextField(), 'a');
await tester.pump();
await tester.enterText(findTextField(), 'ab');
await tester.pump();

requests['ab']!.complete(<Widget>[const Text('ab-result')]);
await tester.pumpAndSettle();
expect(find.text('ab-result'), findsOneWidget);

requests['a']!.complete(<Widget>[const Text('a-result')]);
await tester.pumpAndSettle();
expect(find.text('ab-result'), findsOneWidget);
expect(find.text('a-result'), findsNothing);
});

testWidgets('SearchAnchor.bar has a default search bar as the anchor', (
WidgetTester tester,
) async {
Expand Down