Skip to content

Commit 7695582

Browse files
set onChangedField function to only update options with new results (#150776)
Added function and result identifiers into the RawAutocomplete onChangedField method, now the options value will not be updated if the data is from an optionsBuilder call older than the last call which updated the options *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* Issue #150774 is fixed by this PR *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
1 parent 72ebfc7 commit 7695582

2 files changed

Lines changed: 289 additions & 6 deletions

File tree

‎packages/flutter/lib/src/widgets/autocomplete.dart‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,23 +353,36 @@ class _RawAutocompleteState<T extends Object> extends State<RawAutocomplete<T>>
353353
}
354354
}
355355

356+
// Assigning an ID to every call of _onChangedField is necessary to avoid a
357+
// situation where _options is updated by an older call when multiple
358+
// _onChangedField calls are running simultaneously.
359+
int _onChangedCallId = 0;
356360
// Called when _textEditingController changes.
357361
Future<void> _onChangedField() async {
358362
final TextEditingValue value = _textEditingController.value;
363+
364+
// Makes sure that options change only when content of the field changes.
365+
bool shouldUpdateOptions = false;
366+
if (value.text != _lastFieldText) {
367+
shouldUpdateOptions = true;
368+
_onChangedCallId += 1;
369+
}
370+
_lastFieldText = value.text;
371+
final int callId = _onChangedCallId;
359372
final Iterable<T> options = await widget.optionsBuilder(value);
373+
374+
// Makes sure that previous call results do not replace new ones.
375+
if (callId != _onChangedCallId || !shouldUpdateOptions) {
376+
return;
377+
}
360378
_options = options;
361379
_updateHighlight(_highlightedOptionIndex.value);
362380
final T? selection = _selection;
363381
if (selection != null && value.text != widget.displayStringForOption(selection)) {
364382
_selection = null;
365383
}
366384

367-
// Make sure the options are no longer hidden if the content of the field
368-
// changes (ignore selection changes).
369-
if (value.text != _lastFieldText) {
370-
_lastFieldText = value.text;
371-
_updateOptionsViewVisibility();
372-
}
385+
_updateOptionsViewVisibility();
373386
}
374387

375388
// Called from fieldViewBuilder when the user submits the field.

‎packages/flutter/test/widgets/autocomplete_test.dart‎

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,4 +1288,274 @@ void main() {
12881288
await tester.pump();
12891289
expect(find.byKey(optionsKey), findsNothing);
12901290
});
1291+
1292+
testWidgets('can prevent older optionsBuilder results from replacing the new ones', (WidgetTester tester) async {
1293+
final GlobalKey fieldKey = GlobalKey();
1294+
final GlobalKey optionsKey = GlobalKey();
1295+
late FocusNode focusNode;
1296+
late TextEditingController textEditingController;
1297+
Iterable<String>? lastOptions;
1298+
Duration? delay;
1299+
1300+
await tester.pumpWidget(
1301+
MaterialApp(
1302+
home: Scaffold(
1303+
body: RawAutocomplete<String>(
1304+
optionsBuilder: (TextEditingValue textEditingValue) async {
1305+
final Iterable<String> options = kOptions.where((String option) {
1306+
return option.contains(textEditingValue.text.toLowerCase());
1307+
});
1308+
if (delay == null) {
1309+
return options;
1310+
}
1311+
return Future<Iterable<String>>.delayed(delay, () => options);
1312+
},
1313+
fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
1314+
focusNode = fieldFocusNode;
1315+
textEditingController = fieldTextEditingController;
1316+
return TextField(
1317+
key: fieldKey,
1318+
focusNode: focusNode,
1319+
controller: textEditingController,
1320+
);
1321+
},
1322+
optionsViewBuilder: (BuildContext context, AutocompleteOnSelected<String> onSelected, Iterable<String> options) {
1323+
lastOptions = options;
1324+
return Container(key: optionsKey);
1325+
},
1326+
),
1327+
),
1328+
)
1329+
);
1330+
1331+
const Duration longRequestDelay = Duration(milliseconds: 5000);
1332+
const Duration shortRequestDelay = Duration(milliseconds: 1000);
1333+
focusNode.requestFocus();
1334+
1335+
// Enter the first letter.
1336+
delay = longRequestDelay;
1337+
await tester.enterText(find.byKey(fieldKey), 'c');
1338+
await tester.pump();
1339+
expect(lastOptions, null);
1340+
1341+
// Enter the second letter which resolves faster.
1342+
delay = shortRequestDelay;
1343+
await tester.enterText(find.byKey(fieldKey), 'ch');
1344+
await tester.pump();
1345+
expect(lastOptions, null);
1346+
1347+
// Wait for the short request to resolve.
1348+
await tester.pump(shortRequestDelay);
1349+
1350+
// lastOptions must contain results from the last request.
1351+
expect(find.byKey(optionsKey), findsOneWidget);
1352+
expect(lastOptions, <String>['chameleon']);
1353+
1354+
// Wait for the last timer to finish.
1355+
await tester.pump(longRequestDelay - shortRequestDelay);
1356+
expect(lastOptions, <String>['chameleon']);
1357+
});
1358+
1359+
testWidgets('updates result only from the last call made to optionsBuilder', (WidgetTester tester) async {
1360+
final GlobalKey fieldKey = GlobalKey();
1361+
final GlobalKey optionsKey = GlobalKey();
1362+
late FocusNode focusNode;
1363+
late TextEditingController textEditingController;
1364+
Iterable<String>? lastOptions;
1365+
Duration? delay;
1366+
1367+
await tester.pumpWidget(
1368+
MaterialApp(
1369+
home: Scaffold(
1370+
body: RawAutocomplete<String>(
1371+
optionsBuilder: (TextEditingValue textEditingValue) async {
1372+
final Iterable<String> options = kOptions.where((String option) {
1373+
return option.contains(textEditingValue.text.toLowerCase());
1374+
});
1375+
if (delay == null) {
1376+
return options;
1377+
}
1378+
return Future<Iterable<String>>.delayed(delay, () => options);
1379+
},
1380+
fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
1381+
focusNode = fieldFocusNode;
1382+
textEditingController = fieldTextEditingController;
1383+
return TextField(
1384+
key: fieldKey,
1385+
focusNode: focusNode,
1386+
controller: textEditingController,
1387+
);
1388+
},
1389+
optionsViewBuilder: (BuildContext context, AutocompleteOnSelected<String> onSelected, Iterable<String> options) {
1390+
lastOptions = options;
1391+
return Container(key: optionsKey);
1392+
},
1393+
),
1394+
),
1395+
)
1396+
);
1397+
1398+
focusNode.requestFocus();
1399+
const Duration firstRequestDelay = Duration(milliseconds: 1000);
1400+
const Duration secondRequestDelay = Duration(milliseconds: 2000);
1401+
const Duration thirdRequestDelay = Duration(milliseconds: 3000);
1402+
1403+
// Enter the first letter.
1404+
delay = firstRequestDelay;
1405+
await tester.enterText(find.byKey(fieldKey), 'l');
1406+
await tester.pump();
1407+
expect(lastOptions, null);
1408+
1409+
// Enter the second letter which resolves slower.
1410+
delay = secondRequestDelay;
1411+
await tester.enterText(find.byKey(fieldKey), 'le');
1412+
await tester.pump();
1413+
expect(lastOptions, null);
1414+
1415+
// Enter the third letter which resolves the slowest.
1416+
delay = thirdRequestDelay;
1417+
await tester.enterText(find.byKey(fieldKey), 'lem');
1418+
await tester.pump();
1419+
expect(lastOptions, null);
1420+
1421+
// lastOptions should get updated only from the last request.
1422+
await tester.pump(firstRequestDelay);
1423+
expect(find.byKey(optionsKey), findsNothing);
1424+
expect(lastOptions, null);
1425+
1426+
await tester.pump(secondRequestDelay - firstRequestDelay);
1427+
expect(find.byKey(optionsKey), findsNothing);
1428+
expect(lastOptions, null);
1429+
1430+
await tester.pump(thirdRequestDelay - secondRequestDelay);
1431+
expect(find.byKey(optionsKey), findsOneWidget);
1432+
expect(lastOptions, <String>['lemur']);
1433+
});
1434+
1435+
testWidgets('update options view when field input changes return to the starting keyword', (WidgetTester tester) async {
1436+
final GlobalKey fieldKey = GlobalKey();
1437+
final GlobalKey optionsKey = GlobalKey();
1438+
late FocusNode focusNode;
1439+
late TextEditingController textEditingController;
1440+
Iterable<String>? lastOptions;
1441+
const Duration delay = Duration(milliseconds: 100);
1442+
1443+
await tester.pumpWidget(
1444+
MaterialApp(
1445+
home: Scaffold(
1446+
body: RawAutocomplete<String>(
1447+
optionsBuilder: (TextEditingValue textEditingValue) async {
1448+
final Iterable<String> options = kOptions.where((String option) {
1449+
return option.contains(textEditingValue.text.toLowerCase());
1450+
});
1451+
return Future<Iterable<String>>.delayed(delay, () => options);
1452+
},
1453+
fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
1454+
focusNode = fieldFocusNode;
1455+
textEditingController = fieldTextEditingController;
1456+
return TextField(
1457+
key: fieldKey,
1458+
focusNode: focusNode,
1459+
controller: textEditingController,
1460+
);
1461+
},
1462+
optionsViewBuilder: (BuildContext context, AutocompleteOnSelected<String> onSelected, Iterable<String> options) {
1463+
lastOptions = options;
1464+
return Container(key: optionsKey);
1465+
},
1466+
),
1467+
),
1468+
)
1469+
);
1470+
1471+
// Setup starting point.
1472+
await tester.enterText(find.byKey(fieldKey), 'ele');
1473+
await tester.pump(delay);
1474+
expect(lastOptions, <String>['chameleon', 'elephant']);
1475+
expect(find.byKey(optionsKey), findsOneWidget);
1476+
1477+
// Hide options.
1478+
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
1479+
await tester.pump();
1480+
expect(find.byKey(optionsKey), findsNothing);
1481+
1482+
// Enter another letter and then immediately erase it.
1483+
await tester.enterText(find.byKey(fieldKey), 'eleo');
1484+
await tester.pump();
1485+
expect(find.byKey(optionsKey), findsNothing);
1486+
1487+
await tester.enterText(find.byKey(fieldKey), 'ele');
1488+
await tester.pump(delay);
1489+
expect(lastOptions, <String>['chameleon', 'elephant']);
1490+
1491+
// Options dropdown should be visible after the last optionsBuilder
1492+
// call is resolved.
1493+
expect(find.byKey(optionsKey), findsOneWidget);
1494+
});
1495+
1496+
testWidgets('optionsBuilder does not have to be a pure function', (WidgetTester tester) async {
1497+
final GlobalKey fieldKey = GlobalKey();
1498+
final GlobalKey optionsKey = GlobalKey();
1499+
late FocusNode focusNode;
1500+
late TextEditingController textEditingController;
1501+
Iterable<String>? lastOptions;
1502+
const Duration delay = Duration(milliseconds: 100);
1503+
1504+
// This is used to tell optionsBuilder to return something different after
1505+
// being called with "ele" the second time. I.e. it is not a pure function.
1506+
int timesOptionsBuilderCalledWithEle = 0;
1507+
final Iterable<String> altEleOptions = <String>['something new and crazy for ele!'];
1508+
1509+
await tester.pumpWidget(
1510+
MaterialApp(
1511+
home: Scaffold(
1512+
body: RawAutocomplete<String>(
1513+
optionsBuilder: (TextEditingValue textEditingValue) async {
1514+
if (textEditingValue.text == 'ele') {
1515+
timesOptionsBuilderCalledWithEle += 1;
1516+
if (timesOptionsBuilderCalledWithEle > 1) {
1517+
return Future<Iterable<String>>.delayed(delay, () => altEleOptions);
1518+
}
1519+
}
1520+
final Iterable<String> options = kOptions.where((String option) {
1521+
return option.contains(textEditingValue.text.toLowerCase());
1522+
});
1523+
return Future<Iterable<String>>.delayed(delay, () => options);
1524+
},
1525+
fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
1526+
focusNode = fieldFocusNode;
1527+
textEditingController = fieldTextEditingController;
1528+
return TextField(
1529+
key: fieldKey,
1530+
focusNode: focusNode,
1531+
controller: textEditingController,
1532+
);
1533+
},
1534+
optionsViewBuilder: (BuildContext context, AutocompleteOnSelected<String> onSelected, Iterable<String> options) {
1535+
lastOptions = options;
1536+
return Container(key: optionsKey);
1537+
},
1538+
),
1539+
),
1540+
)
1541+
);
1542+
1543+
await tester.enterText(find.byKey(fieldKey), 'ele');
1544+
await tester.pump(delay);
1545+
expect(lastOptions, <String>['chameleon', 'elephant']);
1546+
expect(find.byKey(optionsKey), findsOneWidget);
1547+
1548+
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
1549+
await tester.pump();
1550+
expect(find.byKey(optionsKey), findsNothing);
1551+
1552+
await tester.enterText(find.byKey(fieldKey), 'eleo');
1553+
await tester.pump();
1554+
expect(find.byKey(optionsKey), findsNothing);
1555+
1556+
await tester.enterText(find.byKey(fieldKey), 'ele');
1557+
await tester.pump(delay);
1558+
expect(lastOptions, altEleOptions);
1559+
expect(find.byKey(optionsKey), findsOneWidget);
1560+
});
12911561
}

0 commit comments

Comments
 (0)