Skip to content

Failed assertion when trying to set caret position to text position that is not visible #122443

Open

Description

@lukehutch
════════ Exception caught by scheduler library ═════════════════════════════════
The following assertion was thrown during a scheduler callback:
'package:flutter/src/widgets/editable_text.dart': Failed assertion: line 3549 pos 16: '!composingRange.isValid || composingRange.isCollapsed': is not true.
editable_text.dart:3549
2

Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

When the exception was thrown, this was the stack
#2      EditableTextState._updateComposingRectIfNeeded
editable_text.dart:3549
#3      EditableTextState._updateComposingRectIfNeeded.<anonymous closure>
editable_text.dart:3555
#4      SchedulerBinding._invokeFrameCallback
binding.dart:1289
#5      SchedulerBinding.handleDrawFrame
binding.dart:1227
#6      SchedulerBinding._handleDrawFrame
binding.dart:1076
#7      _invoke (dart:ui/hooks.dart:145:13)
#8      PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:338:5)
#9      _drawFrame (dart:ui/hooks.dart:112:31)
(elided 2 frames from class _AssertionError)

Steps to Reproduce

Create a TextField that does not allow multi-line input (i.e. don't set maxLines: null).

Then set the text of the TextField to a string that contains a newline (so that part of the text will not be visible in the text widget), using setText (shown below).

This method tries to set the caret position to a position past the end of the visible input in this case, which triggers the exception.

  final controller = TextEditingController();

  setText(String text) {
    controller.text = text;
    // Move cursor to end of text
    controller.selection = TextSelection.fromPosition(
      TextPosition(offset: controller.text.length),
    );
  }

Expected results:

Probably the TextPosition should be set to the closest visible position, rather than failing an assertion if the requested position is not visible.

Actual results:

The above error

[luke@fedora clickdating_flutter]$ flutter doctor -v
[✓] Flutter (Channel stable, 3.7.5, on Fedora Linux 36 (Workstation Edition)
    6.1.14-100.fc36.x86_64, locale en_US.utf8)
    • Flutter version 3.7.5 on channel stable at /opt/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c07f788888 (2 weeks ago), 2023-02-22 17:52:33 -0600
    • Engine revision 0f359063c4
    • Dart version 2.19.2
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /home/luke/Android/Sdk
    • Platform android-33, build-tools 33.0.1
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build
      11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • clang version 14.0.5 (Fedora 14.0.5-2.fc36)
    • cmake version 3.25.2
    • ninja version 1.10.2
    • pkg-config version 1.8.0

[✓] Android Studio (version 2021.3)
    • Android Studio at /opt/android-studio
    • Flutter plugin version 71.0.3
    • Dart plugin version 213.7433
    • Java version OpenJDK Runtime Environment (build
      11.0.13+0-b1751.21-8125866)

[✓] VS Code (version 1.76.0)
    • VS Code at /usr/share/code
    • Flutter extension version 3.60.0

[✓] Connected device (3 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 13
      (API 33) (emulator)
    • Linux (desktop)              • linux         • linux-x64      • Fedora
      Linux 36 (Workstation Edition) 6.1.14-100.fc36.x86_64
    • Chrome (web)                 • chrome        • web-javascript • Google
      Chrome 110.0.5481.177

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Activity

  1. darshankawar commented on Mar 13, 2023

    @darshankawar
    Member

    Thanks for the report @lukehutch
    I am unable to replicate this error on latest stable and master version. Based on the steps to replicate you provided, I came up with below runnable code sample with which I couldn't replicate the reported error.

    code sample
    
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      final _controller = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: Center(
              child: TextField(
                controller: _controller,
                decoration: InputDecoration(
                  hintText: setText('This is a sample text to verify \n a bug and try to replicate it as reported \n')
                ),
              ),
            ),
          ),
        );
      }
    
      setText(String text) {
        _controller.text = text;
        // Move cursor to end of text
        _controller.selection = TextSelection.fromPosition(
          TextPosition(offset: _controller.text.length),
        );
      }
    }
    
    

    If above doesn't represent the replication properly, please provide us the correct code sample and steps to replicate that we can use to verify this issue further.

    A similar issue but seems to be related to localized text: #110343

  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Mar 13, 2023
  3. lukehutch commented on Mar 14, 2023

    @lukehutch
    ContributorAuthor

    Sorry, I should have provided the full repro code myself... thanks for trying to recreate this.

    Try replacing the TextField in your example with MyTextField:

    class MyTextField extends StatelessWidget {
      MyTextField({Key? key}) : super(key: key);
    
      final _textEditingController = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        _textEditingController.text = 'Multiline\ntext\ninput';
        _textEditingController.selection =
            TextSelection.collapsed(offset: _textEditingController.text.length);
        return TextField(
          controller: _textEditingController,
          autofocus: true,
        );
    }

    The autofocus: true part is important, so that the caret is already in the text field when you interact with the contents. Do not click on the Android emulator window, just give the window focus by clicking on the titlebar, or use Alt+Tab to focus the emulator. Clicking on the text field seems to correctly set the caret position to the end of the visible text, which eliminates the problem.

    Then, to reproduce the issue, hit the Left arrow key one or more times. This tries to move the cursor position, which is off the visible screen, and gives the error shown in the original bug report.

    Reproduced on Flutter 3.7.7.

  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Mar 14, 2023
  5. darshankawar commented on Mar 14, 2023

    @darshankawar
    Member

    Thanks for the detailed update. Using the code snippet provided above, I was able to replicate the reported error using latest master as well as stable version. However, this error per my verification only occurs on Android, as trying the same steps using iOS simulator doesn't throw the error.

    runnable code sample
    
    
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            body: Center(
              child: MyTextField()
            ),
          ),
        );
      }
    }
    
    class MyTextField extends StatelessWidget {
      MyTextField({Key? key}) : super(key: key);
    
      final _textEditingController = TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        _textEditingController.text = 'Multiline\ntext\ninput';
        _textEditingController.selection =
            TextSelection.collapsed(offset: _textEditingController.text.length);
        return TextField(
          controller: _textEditingController,
          autofocus: true,
        );
      }
    }
    
    
    
    console error log
    
    
    ======== Exception caught by scheduler library =====================================================
    The following assertion was thrown during a scheduler callback:
    'package:flutter/src/widgets/editable_text.dart': Failed assertion: line 3865 pos 14: '!composingRange.isValid || composingRange.isCollapsed': is not true.
    
    
    Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
    In either case, please report this assertion by filing a bug on GitHub:
      https://github.com/flutter/flutter/issues/new?template=2_bug.md
    
    When the exception was thrown, this was the stack: 
    #2      EditableTextState._updateComposingRectIfNeeded (package:flutter/src/widgets/editable_text.dart:3865:14)
    #3      EditableTextState._schedulePeriodicPostFrameCallbacks (package:flutter/src/widgets/editable_text.dart:3788:5)
    #4      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1286:15)
    #5      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1225:9)
    #6      SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1074:5)
    #7      _invoke (dart:ui/hooks.dart:142:13)
    #8      PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:359:5)
    #9      _drawFrame (dart:ui/hooks.dart:112:31)
    (elided 2 frames from class _AssertionError)
    ====================================================================================================
    
    

    Exact steps to replicate:

    1. Run the code sample above on Android emulator. Note the caret position which should be at the end of the text Multiline.
    2. Do not tap on textfield, but just tap anywhere on emulator to make sure the caret position or cursor is not changed.
    3. Tap on left arrow key few times that leads to the app throwing the error in the console.

    Also observed that, in subsequent tries to replicate, we don't need to repeat step 2 and 3. Upon hot restarting the app, it throws the error directly.

    stable, master flutter doctor -v
    [!] Flutter (Channel stable, 3.7.6, on macOS 12.2.1 21D62 darwin-x64, locale
        en-GB)
        • Flutter version 3.7.6 on channel stable at
          /Users/dhs/documents/fluttersdk/flutter
        ! Warning: `flutter` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
          your current Flutter SDK checkout at
          /Users/dhs/documents/fluttersdk/flutter. Consider adding
          /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
          Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 12cb4eb7a0 (8 days ago), 2023-03-01 10:29:26 -0800
        • Engine revision ada363ee93
        • Dart version 2.19.3
        • DevTools version 2.20.1
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    [!] Flutter (Channel master, 3.9.0-1.0.pre.183, on macOS 12.2.1 21D62
        darwin-x64, locale en-GB)
        • Flutter version 3.9.0-1.0.pre.183 on channel master at
          /Users/dhs/documents/fluttersdk/flutter
        ! Warning: `flutter` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside
          your current Flutter SDK checkout at
          /Users/dhs/documents/fluttersdk/flutter. Consider adding
          /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to
          /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter.
          Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front
          of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 713842fb5b (2 hours ago), 2023-03-13 22:36:11 -0400
        • Engine revision 0fca429de5
        • Dart version 3.0.0 (build 3.0.0-322.0.dev)
        • DevTools version 2.22.2
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
        • Android SDK at /Users/dhs/Library/Android/sdk
        ✗ cmdline-tools component is missing
          Run `path/to/sdkmanager --install "cmdline-tools;latest"`
          See https://developer.android.com/studio/command-line for more details.
        ✗ Android license status unknown.
          Run `flutter doctor --android-licenses` to accept the SDK licenses.
          See https://flutter.dev/docs/get-started/install/macos#android-setup for
          more details.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 13C100
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2)
        • IntelliJ at /Applications/IntelliJ IDEA.app
        • Flutter plugin version 65.1.4
        • Dart plugin version 213.7228
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.29.0
    
    [✓] Connected device (3 available)
        • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios
          • iOS 15.3.1 19D52
        • macOS (desktop)           • macos                                    •
          darwin-x64     • macOS 12.2.1 21D62 darwin-x64
        • Chrome (web)              • chrome                                   •
          web-javascript • Google Chrome 109.0.5414.119
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
          
    [!] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        ! Flutter recommends a minimum Xcode version of 13.
          Download the latest version or update via the Mac App Store.
        • CocoaPods version 1.11.2
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.62.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (5 available)
        • SM G975F (mobile)       • RZ8M802WY0X • android-arm64   • Android 11 (API 30)
        • Darshan's iphone (mobile)  • 21150b119064aecc249dfcfe05e259197461ce23 •
          ios            • iOS 14.4.1 18D61
        • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729     •
          ios            • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator)
        • macOS (desktop)            • macos                                    •
          darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)               • chrome                                   •
          web-javascript • Google Chrome 98.0.4758.80
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    
    
    
  6. added
    a: text inputEntering text in a text field or keyboard related problems
    c: crashStack traces logged to the console
    a: error messageError messages from the Flutter framework
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    frameworkflutter/packages/flutter repository. See also f: labels.
    platform-androidAndroid applications specifically
    and removed
    in triagePresently being triaged by the triage team
    on Mar 14, 2023
  7. 13 remaining items

  8. flutter-triage-bot commented on Aug 8, 2023

    @flutter-triage-bot

    The fyi-framework label is redundant with the team-framework label.

  9. tspence commented on Aug 27, 2025

    @tspence

    Checking in - my team has hit this issue recently in our Android mobile app editor, and I'm curious if this is on the schedule for a fix, or whether there's a workaround we can use to prevent it?

  10. Renzo-Olivares commented on Aug 27, 2025

    @Renzo-Olivares
    Contributor

    Hello @tspence are you still able to reproduce this issue using the sample code in #122443 (comment)? If so what branch are you on. I tried reproducing this myself on the master branch using the sample code but was not able to reproduce the assertion error. There is still potentially an issue where the text position gets set to a non-visible position, but I am not seeing failed assertions on my end.

  11. tspence commented on Aug 28, 2025

    @tspence

    Thanks so much for the quick reply @Renzo-Olivares ! Our issue is the "caret set beyond end of text" issue. We're using flutter and one of our QA analysts demonstrated editing in a text box and scrolling a selection beyond the end of the text, which generated a grey screen. I'll ask the developers to share some more details here as soon as possible.

  12. Renzo-Olivares commented on Aug 28, 2025

    @Renzo-Olivares
    Contributor

    Hi @tspence thanks for the clarification, just to make sure I understand correctly the issue your experiencing is that the caret is being set beyond the end of the visible text, but you are not experiencing an assertion error being triggered. Thank you for any additional details!

  13. LongCatIsLooong commented on Aug 29, 2025

    @LongCatIsLooong
    Contributor

    The assert is removed because it had false positives: #110343.

  14. Renzo-Olivares commented on Aug 29, 2025

    @Renzo-Olivares
    Contributor

    @LongCatIsLooong Thank you for the additional context!

  15. added
    packageflutter/packages repository. See also p: labels.
    and removed
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Aug 17, 2026
  16. added
    team-designOwned by Design Languages team
    triaged-designTriaged by Design Languages team
    and removed on Aug 26, 2026
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: error messageError messages from the Flutter frameworka: text inputEntering text in a text field or keyboard related problemsc: crashStack traces logged to the consolec: new featureNothing broken; request for a new capabilityfound in release: 3.7Found to occur in 3.7found in release: 3.9Found to occur in 3.9has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagespackageflutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyteam-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