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.
diff --git a/examples/api/lib/material/autocomplete/autocomplete.0.dart b/examples/api/lib/material/autocomplete/autocomplete.0.dart
index 21478ca..6c3236a 100644
--- a/examples/api/lib/material/autocomplete/autocomplete.0.dart
+++ b/examples/api/lib/material/autocomplete/autocomplete.0.dart
@@ -18,8 +18,14 @@
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(),
+ ],
+ ),
),
),
);
diff --git a/examples/api/lib/material/autocomplete/autocomplete.1.dart b/examples/api/lib/material/autocomplete/autocomplete.1.dart
index 7e3798e..abee231 100644
--- a/examples/api/lib/material/autocomplete/autocomplete.1.dart
+++ b/examples/api/lib/material/autocomplete/autocomplete.1.dart
@@ -18,8 +18,14 @@
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(),
+ ],
+ ),
),
),
);
diff --git a/examples/api/lib/material/autocomplete/autocomplete.2.dart b/examples/api/lib/material/autocomplete/autocomplete.2.dart
index 62d9b38..8508296 100644
--- a/examples/api/lib/material/autocomplete/autocomplete.2.dart
+++ b/examples/api/lib/material/autocomplete/autocomplete.2.dart
@@ -21,8 +21,14 @@
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(),
+ ],
+ ),
),
),
);
diff --git a/examples/api/lib/material/autocomplete/autocomplete.3.dart b/examples/api/lib/material/autocomplete/autocomplete.3.dart
index ff8e24f..60ccea1 100644
--- a/examples/api/lib/material/autocomplete/autocomplete.3.dart
+++ b/examples/api/lib/material/autocomplete/autocomplete.3.dart
@@ -24,8 +24,14 @@
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(),
+ ],
+ ),
),
),
);
@@ -59,7 +65,6 @@
// 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 1c00ad2..638969b 100644
--- a/examples/api/lib/material/autocomplete/autocomplete.4.dart
+++ b/examples/api/lib/material/autocomplete/autocomplete.4.dart
@@ -25,8 +25,15 @@
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(),
+ ],
+ ),
),
),
);
diff --git a/examples/api/test/material/autocomplete/autocomplete.3_test.dart b/examples/api/test/material/autocomplete/autocomplete.3_test.dart
index a0965a4..00e5df1 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 @@
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);
+ });
}