Skip to content

Autocomplete(Flutter 3.24.3), optionsViewBuilder does not get the last options of optionsBuilder #157244

Closed as duplicate

Description

@fufesou

Steps to reproduce

Flutter 3.19.6 works fine, Flutter 3.24.3 may break something.

  1. Run the example code on Windows, Linux, or MacOS.
  2. Click the edit filed.

Expected results

The options view should show three options. "aaa", "abc", "bcc"

Actual results

It show one option "empty".

Code sample

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

void main() => runApp(const AutocompleteExampleApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text(
              'Autocomplete - optionsBuilder and optionsViewBuilder'),
        ),
        body: const Center(
          child: _AsyncAutocomplete(),
        ),
      ),
    );
  }
}

class _AsyncAutocomplete extends StatefulWidget {
  const _AsyncAutocomplete();

  @override
  State<_AsyncAutocomplete> createState() => _AsyncAutocompleteState();
}

class _AsyncAutocompleteState extends State<_AsyncAutocomplete> {
  Iterable<String> _options = <String>['empty'];

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Autocomplete<String>(
      optionsBuilder: (TextEditingValue textEditingValue) async {
        debugPrint('================= optionsBuilder, options: $_options');
        if (_options.length == 1) {
          return <String>['empty'];
        } else {
          return [..._options];
        }
      },
      fieldViewBuilder: (
        BuildContext context,
        TextEditingController fieldTextEditingController,
        FocusNode fieldFocusNode,
        VoidCallback onFieldSubmitted,
      ) {
        fieldTextEditingController.text = 'aaa';
        fieldFocusNode.addListener(() async {
          if (fieldFocusNode.hasFocus && _options.length == 1) {
            setState(() {
              _options = <String>['aaa', 'abc', 'bcc'];
            });
          }
        });
        return TextField(
          focusNode: fieldFocusNode,
          controller: fieldTextEditingController,
        );
      },
      optionsViewBuilder: (BuildContext context,
          AutocompleteOnSelected<String> onSelected, Iterable<String> options) {
        debugPrint('================= optionsViewBuilder, options: $options');
        return Material(
          elevation: 4.0,
          child: ListView(
            children: options
                .map((String option) => GestureDetector(
                      onTap: () {
                        onSelected(option);
                      },
                      child: ListTile(
                        title: Text(option),
                      ),
                    ))
                .toList(),
          ),
        );
      },
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs, flutter 3.24.3
flutter: ================= optionsBuilder, options: [empty]
flutter: ================= optionsBuilder, options: [aaa, abc, bcc]
flutter: ================= optionsBuilder, options: [aaa, abc, bcc]
flutter: ================= optionsViewBuilder, options: [empty]
Logs, flutter 3.19.6
flutter: ================= optionsBuilder, options: [empty]
flutter: ================= optionsViewBuilder, options: [empty]
flutter: ================= optionsBuilder, options: [aaa, abc, bcc]
flutter: ================= optionsBuilder, options: [aaa, abc, bcc]
flutter: ================= optionsViewBuilder, options: [aaa, abc, bcc]

Flutter Doctor output

Doctor output
[√] Flutter (Channel stable, 3.24.3, on Microsoft Windows [Version 10.0.22631.4317], locale en-US)
    • Flutter version 3.24.3 on channel stable at D:\DevEnv\flutter\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2663184aa7 (6 weeks ago), 2024-09-11 16:27:48 -0500
    • Engine revision 36335019a8
    • Dart version 3.5.3
    • DevTools version 2.37.3

[√] Windows Version (Installed version of Windows is version 10 or higher)

[!] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at C:\Users\Administrator\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/to/windows-android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.10.3)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.10.35013.160
    • Windows 10 SDK version 10.0.26100.0

[√] Android Studio (version 2024.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0--11609105)

[√] VS Code (version 1.94.2)
    • VS Code at C:\Users\Administrator\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.98.0

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

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22631.4317]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 129.0.6668.101
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 130.0.2849.46

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.

Activity

  1. added
    in triagePresently being triaged by the triage team
    on Oct 21, 2024
  2. huycozy commented on Oct 21, 2024

    @huycozy
    Member

    Thanks for the report. I can reproduce this issue on Flutter master channel 3.27.0-1.0.pre.120 and the previous stable release 3.22.3.

  3. added
    a: text inputEntering text in a text field or keyboard related problems
    c: regressionIt was better in the past than it is now
    frameworkflutter/packages/flutter repository. See also f: labels.
    p: material_uimaterial_ui package in flutter/packages
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    team-designOwned by Design Languages team
    and removed
    in triagePresently being triaged by the triage team
    on Oct 21, 2024
  4. huycozy commented on Oct 21, 2024

    @huycozy
    Member

    Bisected to PR #140285, cc @LongCatIsLooong for thoughts.

  5. added and removed
    team-designOwned by Design Languages team
    on Oct 23, 2024
  6. added
    P2Important issues not at the top of the work list
    on Oct 24, 2024
  7. AncientPixel commented on Feb 4, 2025

    @AncientPixel

    This is a production issue for us, is there any movement on this issue?

  8. md-weber commented on Feb 5, 2025

    @md-weber
    Contributor

    We tested a bit more and found that in Android 15 it works fine, in Android 13 + 14 it is broken. Also in iOS we did not have the issues.

    The issue still persists in Flutter 3.27.1

    Our workaround for now it to take the pre patched version into our own project and link it with the autocomplete.

  9. added
    c: new featureNothing broken; request for a new capability
    and removed
    c: regressionIt was better in the past than it is now
    on Feb 25, 2025
  10. LongCatIsLooong commented on Feb 25, 2025

    @LongCatIsLooong
    Contributor

    After reading the API docs / implementation, I think the current API / implementation expect optionsBuilder to be a "closed". In other words, it must not capture free variables, and the output should only depend on the input textEditingValue. Existing issue for forcing an optionsBuilder re-evaluation: #159443

    In other other words:

                setState(() {
                  _options = <String>['aaa', 'abc', 'bcc'];
                });

    does not force the implementation to re-evaluate optionsBuilder, and it shouldn't. The existing API needs to be updated.

    The way it "worked" before is very fragile so I don't think that should be restored - it would cause more issues than it would solve.

  11. added
    r: duplicateIssue is closed as a duplicate of an existing issue
    on Feb 25, 2025
  12. github-actions commented on Mar 11, 2025

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  13. locked as resolved and limited conversation to collaborators on Mar 11, 2025
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 lista: text inputEntering text in a text field or keyboard related problemsc: new featureNothing broken; request for a new capabilityfound in release: 3.24Found to occur in 3.24found in release: 3.27Found to occur in 3.27frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesr: duplicateIssue is closed as a duplicate of an existing issueteam-text-inputOwned by Text Input teamtriaged-text-inputTriaged by Text Input team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions