From f6f5bb9023af3569626c6c6d7b2deb646bd716dd Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Mon, 22 May 2023 09:55:21 -0700 Subject: [PATCH] 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 https://github.com/flutter/flutter/pull/127019#issuecomment-1552347037 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. --- .../material/autocomplete/autocomplete.0.dart | 10 ++++-- .../material/autocomplete/autocomplete.1.dart | 10 ++++-- .../material/autocomplete/autocomplete.2.dart | 10 ++++-- .../material/autocomplete/autocomplete.3.dart | 11 +++++-- .../material/autocomplete/autocomplete.4.dart | 11 +++++-- .../autocomplete/autocomplete.3_test.dart | 33 +++++++++++++++++++ 6 files changed, 74 insertions(+), 11 deletions(-) diff --git a/examples/api/lib/material/autocomplete/autocomplete.0.dart b/examples/api/lib/material/autocomplete/autocomplete.0.dart index 21478cabb3..6c3236aaab 100644 --- a/examples/api/lib/material/autocomplete/autocomplete.0.dart +++ b/examples/api/lib/material/autocomplete/autocomplete.0.dart @@ -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: [ + Text('Type below to autocomplete the following possible results: ${AutocompleteBasicExample._kOptions}.'), + const AutocompleteBasicExample(), + ], + ), ), ), ); diff --git a/examples/api/lib/material/autocomplete/autocomplete.1.dart b/examples/api/lib/material/autocomplete/autocomplete.1.dart index 7e3798e558..abee231da4 100644 --- a/examples/api/lib/material/autocomplete/autocomplete.1.dart +++ b/examples/api/lib/material/autocomplete/autocomplete.1.dart @@ -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: [ + Text('Type below to autocomplete the following possible results: ${AutocompleteBasicUserExample._userOptions}.'), + const AutocompleteBasicUserExample(), + ], + ), ), ), ); diff --git a/examples/api/lib/material/autocomplete/autocomplete.2.dart b/examples/api/lib/material/autocomplete/autocomplete.2.dart index 62d9b388d1..85082969e8 100644 --- a/examples/api/lib/material/autocomplete/autocomplete.2.dart +++ b/examples/api/lib/material/autocomplete/autocomplete.2.dart @@ -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: [ + Text('Type below to autocomplete the following possible results: ${_FakeAPI._kOptions}.'), + const _AsyncAutocomplete(), + ], + ), ), ), ); diff --git a/examples/api/lib/material/autocomplete/autocomplete.3.dart b/examples/api/lib/material/autocomplete/autocomplete.3.dart index ff8e24f560..60ccea126d 100644 --- a/examples/api/lib/material/autocomplete/autocomplete.3.dart +++ b/examples/api/lib/material/autocomplete/autocomplete.3.dart @@ -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: [ + Text('Type below to autocomplete the following possible results: ${_FakeAPI._kOptions}.'), + const _AsyncAutocomplete(), + ], + ), ), ), ); @@ -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; diff --git a/examples/api/lib/material/autocomplete/autocomplete.4.dart b/examples/api/lib/material/autocomplete/autocomplete.4.dart index 1c00ad22ab..638969b37f 100644 --- a/examples/api/lib/material/autocomplete/autocomplete.4.dart +++ b/examples/api/lib/material/autocomplete/autocomplete.4.dart @@ -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: [ + Text('Type below to autocomplete the following possible results: ${_FakeAPI._kOptions}.'), + const SizedBox(height: 32.0), + const _AsyncAutocomplete(), + ], + ), ), ), ); diff --git a/examples/api/test/material/autocomplete/autocomplete.3_test.dart b/examples/api/test/material/autocomplete/autocomplete.3_test.dart index a0965a4908..00e5df1afc 100644 --- a/examples/api/test/material/autocomplete/autocomplete.3_test.dart +++ b/examples/api/test/material/autocomplete/autocomplete.3_test.dart @@ -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); + }); }