Skip to content

Commit ccd22e1

Browse files
authored
[material_ui] Prevent stale async suggestions in SearchAnchor (flutter#12478)
Ports flutter#190206 to `material_ui` following flutter#188444. Fixes flutter#190205. This change prevents an older asynchronous `SearchAnchor.suggestionsBuilder` request from replacing the results of a newer request. It assigns an incrementing ID to each suggestions request and only applies the result when it still represents the latest request. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 99719ff commit ccd22e1

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

‎packages/material_ui/lib/src/search_anchor.dart‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,8 @@ class _ViewContentState extends State<_ViewContent> {
974974
Iterable<Widget> result = <Widget>[];
975975
String? searchValue;
976976
Timer? _timer;
977+
// Identifies the latest call so that older async results cannot replace newer ones.
978+
int _suggestionsCallId = 0;
977979

978980
@override
979981
void initState() {
@@ -1010,14 +1012,9 @@ class _ViewContentState extends State<_ViewContent> {
10101012
_timer?.cancel();
10111013
_timer = Timer(Duration.zero, () async {
10121014
searchValue = _controller.text;
1013-
final Iterable<Widget> suggestions = await widget.suggestionsBuilder(context, _controller);
1015+
await _buildSuggestions();
10141016
_timer?.cancel();
10151017
_timer = null;
1016-
if (mounted) {
1017-
setState(() {
1018-
result = suggestions;
1019-
});
1020-
}
10211018
});
10221019
}
10231020
}
@@ -1058,15 +1055,21 @@ class _ViewContentState extends State<_ViewContent> {
10581055
Future<void> updateSuggestions() async {
10591056
if (searchValue != _controller.text) {
10601057
searchValue = _controller.text;
1061-
final Iterable<Widget> suggestions = await widget.suggestionsBuilder(context, _controller);
1062-
if (mounted) {
1063-
setState(() {
1064-
result = suggestions;
1065-
});
1066-
}
1058+
await _buildSuggestions();
10671059
}
10681060
}
10691061

1062+
Future<void> _buildSuggestions() async {
1063+
final int callId = ++_suggestionsCallId;
1064+
final Iterable<Widget> suggestions = await widget.suggestionsBuilder(context, _controller);
1065+
if (!mounted || callId != _suggestionsCallId) {
1066+
return;
1067+
}
1068+
setState(() {
1069+
result = suggestions;
1070+
});
1071+
}
1072+
10701073
@override
10711074
Widget build(BuildContext context) {
10721075
final Widget defaultLeading = BackButton(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Prevents stale asynchronous suggestions from replacing newer `SearchAnchor` results.
3+
version: patch

‎packages/material_ui/test/search_anchor_test.dart‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,46 @@ void main() {
22612261
expect(controller.value.text, suggestion);
22622262
});
22632263

2264+
testWidgets('SearchAnchor ignores out-of-order async suggestions', (WidgetTester tester) async {
2265+
final requests = <String, Completer<Iterable<Widget>>>{};
2266+
2267+
await tester.pumpWidget(
2268+
MaterialApp(
2269+
home: SearchAnchor(
2270+
builder: (BuildContext context, SearchController controller) {
2271+
return const Icon(Icons.search);
2272+
},
2273+
suggestionsBuilder: (BuildContext context, SearchController controller) {
2274+
final String query = controller.text;
2275+
if (query.isEmpty) {
2276+
return <Widget>[];
2277+
}
2278+
final request = Completer<Iterable<Widget>>();
2279+
requests[query] = request;
2280+
return request.future;
2281+
},
2282+
),
2283+
),
2284+
);
2285+
2286+
await tester.tap(find.byIcon(Icons.search));
2287+
await tester.pumpAndSettle();
2288+
2289+
await tester.enterText(findTextField(), 'a');
2290+
await tester.pump();
2291+
await tester.enterText(findTextField(), 'ab');
2292+
await tester.pump();
2293+
2294+
requests['ab']!.complete(<Widget>[const Text('ab-result')]);
2295+
await tester.pumpAndSettle();
2296+
expect(find.text('ab-result'), findsOneWidget);
2297+
2298+
requests['a']!.complete(<Widget>[const Text('a-result')]);
2299+
await tester.pumpAndSettle();
2300+
expect(find.text('ab-result'), findsOneWidget);
2301+
expect(find.text('a-result'), findsNothing);
2302+
});
2303+
22642304
testWidgets('SearchAnchor.bar has a default search bar as the anchor', (
22652305
WidgetTester tester,
22662306
) async {

0 commit comments

Comments
 (0)