Skip to content

Commit

Permalink
Fix bug in Autocomplete example (flutter#127219)
Browse files Browse the repository at this point in the history
This example was incorrectly throwing away results from a query when multiple queries were pending at once.   Thanks to @sun-jiao in 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.
  • Loading branch information
justinmc authored May 22, 2023
1 parent 5b0615c commit f6f5bb9
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 11 deletions.
10 changes: 8 additions & 2 deletions examples/api/lib/material/autocomplete/autocomplete.0.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ class AutocompleteExampleApp extends StatelessWidget {
appBar: AppBar(
title: const Text('Autocomplete Basic'),
),
body: const Center(
child: AutocompleteBasicExample(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Type below to autocomplete the following possible results: ${AutocompleteBasicExample._kOptions}.'),
const AutocompleteBasicExample(),
],
),
),
),
);
Expand Down
10 changes: 8 additions & 2 deletions examples/api/lib/material/autocomplete/autocomplete.1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ class AutocompleteExampleApp extends StatelessWidget {
appBar: AppBar(
title: const Text('Autocomplete Basic User'),
),
body: const Center(
child: AutocompleteBasicUserExample(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Type below to autocomplete the following possible results: ${AutocompleteBasicUserExample._userOptions}.'),
const AutocompleteBasicUserExample(),
],
),
),
),
);
Expand Down
10 changes: 8 additions & 2 deletions examples/api/lib/material/autocomplete/autocomplete.2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ class AutocompleteExampleApp extends StatelessWidget {
appBar: AppBar(
title: const Text('Autocomplete - async'),
),
body: const Center(
child: _AsyncAutocomplete(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Type below to autocomplete the following possible results: ${_FakeAPI._kOptions}.'),
const _AsyncAutocomplete(),
],
),
),
),
);
Expand Down
11 changes: 8 additions & 3 deletions examples/api/lib/material/autocomplete/autocomplete.3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ class AutocompleteExampleApp extends StatelessWidget {
appBar: AppBar(
title: const Text('Autocomplete - async and debouncing'),
),
body: const Center(
child: _AsyncAutocomplete(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Type below to autocomplete the following possible results: ${_FakeAPI._kOptions}.'),
const _AsyncAutocomplete(),
],
),
),
),
);
Expand Down Expand Up @@ -59,7 +65,6 @@ class _AsyncAutocompleteState extends State<_AsyncAutocomplete > {

// If another search happened after this one, throw away these options.
if (_currentQuery != query) {
_currentQuery = null;
return null;
}
_currentQuery = null;
Expand Down
11 changes: 9 additions & 2 deletions examples/api/lib/material/autocomplete/autocomplete.4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ class AutocompleteExampleApp extends StatelessWidget {
appBar: AppBar(
title: const Text('Autocomplete - async, debouncing, and network errors'),
),
body: const Center(
child: _AsyncAutocomplete(),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Type below to autocomplete the following possible results: ${_FakeAPI._kOptions}.'),
const SizedBox(height: 32.0),
const _AsyncAutocomplete(),
],
),
),
),
);
Expand Down
33 changes: 33 additions & 0 deletions examples/api/test/material/autocomplete/autocomplete.3_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,37 @@ void main() {
expect(find.text('bobcat'), findsNothing);
expect(find.text('chameleon'), findsOneWidget);
});

testWidgets('multiple pending requests', (WidgetTester tester) async {
await tester.pumpWidget(const example.AutocompleteExampleApp());

await tester.enterText(find.byType(TextFormField), 'a');

// Wait until the debounce duration has expired, but the request is still
// pending.
await tester.pump(example.debounceDuration);

expect(find.text('aardvark'), findsNothing);
expect(find.text('bobcat'), findsNothing);
expect(find.text('chameleon'), findsNothing);

await tester.enterText(find.byType(TextFormField), 'aa');

// Wait until the first request has completed.
await tester.pump(example.fakeAPIDuration - example.debounceDuration);

// The results from the first request are thrown away since the query has
// changed.
expect(find.text('aardvark'), findsNothing);
expect(find.text('bobcat'), findsNothing);
expect(find.text('chameleon'), findsNothing);

// Wait until the second request has completed.
await tester.pump(example.fakeAPIDuration);

// The results of the second request are reflected.
expect(find.text('aardvark'), findsOneWidget);
expect(find.text('bobcat'), findsNothing);
expect(find.text('chameleon'), findsNothing);
});
}

0 comments on commit f6f5bb9

Please sign in to comment.