Skip to content

Commit f6f5bb9

Browse files
authored
Fix bug in Autocomplete example (#127219)
This example was incorrectly throwing away results from a query when multiple queries were pending at once. Thanks to @sun-jiao in flutter/flutter#127019 (comment) for pointing this out. I also added a quick `Text` widget explaining what to do to use the examples. Since there are only three small possible `options`, it's easy to type into the field and not get any results and wonder what's wrong.
1 parent 5b0615c commit f6f5bb9

File tree

6 files changed

+74
-11
lines changed

6 files changed

+74
-11
lines changed

examples/api/lib/material/autocomplete/autocomplete.0.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ class AutocompleteExampleApp extends StatelessWidget {
1818
appBar: AppBar(
1919
title: const Text('Autocomplete Basic'),
2020
),
21-
body: const Center(
22-
child: AutocompleteBasicExample(),
21+
body: Center(
22+
child: Column(
23+
mainAxisAlignment: MainAxisAlignment.center,
24+
children: <Widget>[
25+
Text('Type below to autocomplete the following possible results: ${AutocompleteBasicExample._kOptions}.'),
26+
const AutocompleteBasicExample(),
27+
],
28+
),
2329
),
2430
),
2531
);

examples/api/lib/material/autocomplete/autocomplete.1.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ class AutocompleteExampleApp extends StatelessWidget {
1818
appBar: AppBar(
1919
title: const Text('Autocomplete Basic User'),
2020
),
21-
body: const Center(
22-
child: AutocompleteBasicUserExample(),
21+
body: Center(
22+
child: Column(
23+
mainAxisAlignment: MainAxisAlignment.center,
24+
children: <Widget>[
25+
Text('Type below to autocomplete the following possible results: ${AutocompleteBasicUserExample._userOptions}.'),
26+
const AutocompleteBasicUserExample(),
27+
],
28+
),
2329
),
2430
),
2531
);

examples/api/lib/material/autocomplete/autocomplete.2.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ class AutocompleteExampleApp extends StatelessWidget {
2121
appBar: AppBar(
2222
title: const Text('Autocomplete - async'),
2323
),
24-
body: const Center(
25-
child: _AsyncAutocomplete(),
24+
body: Center(
25+
child: Column(
26+
mainAxisAlignment: MainAxisAlignment.center,
27+
children: <Widget>[
28+
Text('Type below to autocomplete the following possible results: ${_FakeAPI._kOptions}.'),
29+
const _AsyncAutocomplete(),
30+
],
31+
),
2632
),
2733
),
2834
);

examples/api/lib/material/autocomplete/autocomplete.3.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ class AutocompleteExampleApp extends StatelessWidget {
2424
appBar: AppBar(
2525
title: const Text('Autocomplete - async and debouncing'),
2626
),
27-
body: const Center(
28-
child: _AsyncAutocomplete(),
27+
body: Center(
28+
child: Column(
29+
mainAxisAlignment: MainAxisAlignment.center,
30+
children: <Widget>[
31+
Text('Type below to autocomplete the following possible results: ${_FakeAPI._kOptions}.'),
32+
const _AsyncAutocomplete(),
33+
],
34+
),
2935
),
3036
),
3137
);
@@ -59,7 +65,6 @@ class _AsyncAutocompleteState extends State<_AsyncAutocomplete > {
5965

6066
// If another search happened after this one, throw away these options.
6167
if (_currentQuery != query) {
62-
_currentQuery = null;
6368
return null;
6469
}
6570
_currentQuery = null;

examples/api/lib/material/autocomplete/autocomplete.4.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ class AutocompleteExampleApp extends StatelessWidget {
2525
appBar: AppBar(
2626
title: const Text('Autocomplete - async, debouncing, and network errors'),
2727
),
28-
body: const Center(
29-
child: _AsyncAutocomplete(),
28+
body: Center(
29+
child: Column(
30+
mainAxisAlignment: MainAxisAlignment.center,
31+
children: <Widget>[
32+
Text('Type below to autocomplete the following possible results: ${_FakeAPI._kOptions}.'),
33+
const SizedBox(height: 32.0),
34+
const _AsyncAutocomplete(),
35+
],
36+
),
3037
),
3138
),
3239
);

examples/api/test/material/autocomplete/autocomplete.3_test.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,37 @@ void main() {
7777
expect(find.text('bobcat'), findsNothing);
7878
expect(find.text('chameleon'), findsOneWidget);
7979
});
80+
81+
testWidgets('multiple pending requests', (WidgetTester tester) async {
82+
await tester.pumpWidget(const example.AutocompleteExampleApp());
83+
84+
await tester.enterText(find.byType(TextFormField), 'a');
85+
86+
// Wait until the debounce duration has expired, but the request is still
87+
// pending.
88+
await tester.pump(example.debounceDuration);
89+
90+
expect(find.text('aardvark'), findsNothing);
91+
expect(find.text('bobcat'), findsNothing);
92+
expect(find.text('chameleon'), findsNothing);
93+
94+
await tester.enterText(find.byType(TextFormField), 'aa');
95+
96+
// Wait until the first request has completed.
97+
await tester.pump(example.fakeAPIDuration - example.debounceDuration);
98+
99+
// The results from the first request are thrown away since the query has
100+
// changed.
101+
expect(find.text('aardvark'), findsNothing);
102+
expect(find.text('bobcat'), findsNothing);
103+
expect(find.text('chameleon'), findsNothing);
104+
105+
// Wait until the second request has completed.
106+
await tester.pump(example.fakeAPIDuration);
107+
108+
// The results of the second request are reflected.
109+
expect(find.text('aardvark'), findsOneWidget);
110+
expect(find.text('bobcat'), findsNothing);
111+
expect(find.text('chameleon'), findsNothing);
112+
});
80113
}

0 commit comments

Comments
 (0)