Sitelet https://github.com/flutter/flutter/issues/190205
Skip to content

SearchAnchor can show stale results when async suggestions complete out of order #190205

Description

@cuishuang

Steps to reproduce

  1. Run the code sample below.
  2. Open the search view.
  3. Type a, then immediately type b, so that the current query becomes ab.
  4. Wait for both asynchronous requests to complete.

The request for a intentionally takes 500 ms, while the request for ab takes only 50 ms.

Expected results

Only the suggestions for the latest query, ab, should be displayed.

Actual results

The ab-result suggestion is displayed first, but is later replaced by the stale a-result when the older request completes.

SearchAnchor.suggestionsBuilder accepts FutureOr<Iterable<Widget>>, but _ViewContentState currently applies every completed result as long as the widget is mounted. It does not verify that the completed request is still the most recent request.

This can also occur between the initial suggestions request started from didChangeDependencies and later requests triggered by controller changes.

RawAutocomplete already avoids the same class of race by assigning a call ID to each asynchronous options request.

Code sample

Code sample
import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(home: SearchAnchorRaceExample()));
}

class SearchAnchorRaceExample extends StatelessWidget {
  const SearchAnchorRaceExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: SearchAnchor.bar(
          suggestionsBuilder: (
            BuildContext context,
            SearchController controller,
          ) async {
            final String query = controller.text;
            if (query.isEmpty) {
              return <Widget>[];
            }

            await Future<void>.delayed(
              query == 'a'
                  ? const Duration(milliseconds: 500)
                  : const Duration(milliseconds: 50),
            );

            return <Widget>[
              ListTile(title: Text('$query-result')),
            ];
          },
        ),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.41.5, on macOS 26.0 25A8353 darwin-arm64, locale zh-Hans-CN) [88ms]
    • Flutter version 3.41.5 on channel stable at /opt/homebrew/share/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2c9eb20739 (4 months ago), 2026-03-17 16:14:01 -0700
    • Engine revision 052f31d115
    • Dart version 3.11.3
    • DevTools version 2.54.2
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn
    • Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios,
      cli-animations, enable-native-assets, omit-legacy-version-file, enable-lldb-debugging, enable-uiscene-migration

[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [610ms]
    • Android SDK at /Users/mac/Library/Android/sdk
    • Emulator version 36.6.11.0 (build_id 15507667) (CL:N/A)
    • Platform android-36.1, build-tools 36.0.0
    • ANDROID_HOME = /Users/mac/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.10+-117844308-b1163.108)
    • All Android licenses accepted.

[✗] Xcode - develop for iOS and macOS [111ms]
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS and macOS development.
      Download at: https://developer.apple.com/xcode/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods installed but not working.
        You appear to have CocoaPods installed but it is not working.
        This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used to invoke it.
        This can usually be fixed by re-installing CocoaPods.
      For re-installation instructions, see https://guides.cocoapods.org/using/getting-started.html#installation

[✓] Chrome - develop for the web [24ms]
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Proxy Configuration [23ms]
    • HTTP_PROXY is set
    ! NO_PROXY is not set

[✓] Connected device (2 available) [123ms]
    • macOS (desktop) • macos  • darwin-arm64   • macOS 26.0 25A8353 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 150.0.7871.187

[✓] Network resources [1,050ms]
    • All expected network resources are available.

! Doctor found issues in 2 categories.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listp: material_uimaterial_ui package in flutter/packagespackageflutter/packages repository. See also p: labels.team-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions