Skip to content

debugNeedsLayout assertion failed in background page under SelectableRegion #119772

Description

@alexeyinkin

Steps to Reproduce

  1. Create a new Flutter project.
  2. Replace main.dart with the following.
  3. Run in Chrome.

Expected results: "foreground" text is visible, no exceptions

Actual results: "foreground" text is visible, an exception in console:

======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
Assertion failed: file:///home/alexey/snap/flutter/common/flutter/packages/flutter/lib/src/rendering/paragraph.dart:1001:12
!debugNeedsLayout
is not true

When the exception was thrown, this was the stack: 
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49  throw_
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 29:3    assertFailed
packages/flutter/src/rendering/paragraph.dart 1001:13                         getBoxesForSelection
packages/flutter/src/rendering/paragraph.dart 1728:45                         get [_rect]
packages/flutter/src/rendering/paragraph.dart 1702:44                         getTransformToParagraph
packages/flutter/src/rendering/paragraph.dart 1707:12                         getTransformTo
packages/flutter/src/widgets/selectable_region.dart 1599:8                    [_compareScreenOrder]
dart-sdk/lib/internal/sort.dart 69:51                                         _insertionSort
dart-sdk/lib/internal/sort.dart 58:7                                          _doSort
dart-sdk/lib/internal/sort.dart 33:5                                          sort
dart-sdk/lib/_internal/js_dev_runtime/private/js_array.dart 460:12            sort]
packages/flutter/src/widgets/selectable_region.dart 1506:70                   <fn>
packages/flutter/src/widgets/selectable_region.dart 1506:75                   [_flushAdditions]
packages/flutter/src/widgets/selectable_region.dart 1500:7                    [_updateSelectables]
packages/flutter/src/widgets/selectable_region.dart 1492:9                    <fn>
packages/flutter/src/scheduler/binding.dart 1289:15                           [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1227:9                            handleDrawFrame
packages/flutter/src/scheduler/binding.dart 942:7                             <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/isolate_helper.dart 48:19       internalCallback
====================================================================================================

The exception is thrown by the background page of Navigator. If one of the pages is deleted, everything works fine. So this is not a problem to the foreground page but it clutters the console and may prevent background pages from some desired background activity.

main.dart
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: OneSelectableRegionPage(),
    );
  }
}

final _focusNode = FocusNode();

class OneSelectableRegionPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SelectableRegion(
      focusNode: _focusNode,
      selectionControls: materialTextSelectionControls,
      child: Navigator(
        pages: [
          MaterialPage(
            child: Scaffold(body: Text('background')),
          ),
          MaterialPage(
            child: Scaffold(body: Text('foreground')),
          ),
        ],
        onPopPage: (_, __) => false,
      ),
    );
  }
}
flutter doctor -v
[✓] Flutter (Channel stable, 3.7.1, on Ubuntu 20.04.5 LTS 5.14.0-1056-oem, locale en_US.UTF-8)
    • Flutter version 3.7.1 on channel stable at /home/alexey/snap/flutter/common/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 7048ed95a5 (9 hours ago), 2023-02-01 09:07:31 -0800
    • Engine revision 800594f1f4
    • Dart version 2.19.1
    • DevTools version 2.20.1

[!] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /home/alexey/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/linux#android-setup for more details.

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

[✓] Linux toolchain - develop for Linux desktop
    • clang version 10.0.0-4ubuntu1
    • cmake version 3.16.3
    • ninja version 1.10.0
    • pkg-config version 0.29.1

[✓] Android Studio (version 2021.2)
    • Android Studio at /home/alexey/bin/android-studio
    • Flutter plugin version 71.0.2
    • Dart plugin version 212.5744
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] VS Code (version 1.74.3)
    • VS Code at /usr/share/code
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Ubuntu 20.04.5 LTS 5.14.0-1056-oem
    • Chrome (web)    • chrome • web-javascript • Google Chrome 109.0.5414.119

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

! Doctor found issues in 1 category.

Activity

  1. alexeyinkin commented on Feb 2, 2023

    @alexeyinkin
    Author
  2. added
    in triagePresently being triaged by the triage team
    on Feb 2, 2023
  3. huycozy commented on Feb 2, 2023

    @huycozy
    Member

    @alexeyinkin Thanks for the report. I also see the exception on Web and other platforms (Android, iOS, MacOS). Verified on the latest stable and master channels.

    flutter doctor -v (stable and master)
    [✓] Flutter (Channel stable, 3.7.1, on macOS 13.0.1 22A400 darwin-x64, locale en-VN)
        • Flutter version 3.7.1 on channel stable at /Users/huynq/Documents/GitHub/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 7048ed95a5 (11 hours ago), 2023-02-01 09:07:31 -0800
        • Engine revision 800594f1f4
        • Dart version 2.19.1
        • DevTools version 2.20.1
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at /Users/huynq/Library/Android/sdk
        • Platform android-33, build-tools 31.0.0
        • ANDROID_HOME = /Users/huynq/Library/Android/sdk
        • Java binary at: /Applications/Android Studio Dolphin 2021.3.1.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14B47b
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.3)
        • Android Studio at /Applications/Android Studio Dolphin 2021.3.1.app/Contents
        • 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 11.0.13+0-b1751.21-8125866)
    
    [!] Android Studio (version 2022.1)
        • Android Studio at /Applications/Android Studio.app/Contents
        • 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
        ✗ Unable to find bundled Java version.
        • Try updating or re-installing Android Studio.
    
    [✓] IntelliJ IDEA Community Edition (version 2022.1.1)
        • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app
        • 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
    
    [✓] VS Code (version 1.74.3)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.58.0
    
    [✓] Connected device (4 available)
        • RMX2001 (mobile) • EUYTFEUSQSRGDA6D                         • android-arm64  • Android 11 (API 30)
        • iPhone (mobile)  • d9a94afe2b649fef56ba0bfeb052f0f2a7dae95e • ios            • iOS 15.7.2 19H218
        • macOS (desktop)  • macos                                    • darwin-x64     • macOS 13.0.1 22A400 darwin-x64
        • Chrome (web)     • chrome                                   • web-javascript • Google Chrome 109.0.5414.119
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 1 category.
    
    [!] Flutter (Channel master, 3.8.0-1.0.pre.32, on macOS 13.0.1 22A400 darwin-x64, locale en-VN)
        • Flutter version 3.8.0-1.0.pre.32 on channel master at /Users/huynq/Documents/GitHub/flutter_master
        ! Warning: `flutter` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
        ! Warning: `dart` on your path resolves to /Users/huynq/Documents/GitHub/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/huynq/Documents/GitHub/flutter_master. Consider adding /Users/huynq/Documents/GitHub/flutter_master/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision f9daa9aac4 (11 minutes ago), 2023-02-01 22:59:24 -0500
        • Engine revision 679c4b42e2
        • Dart version 3.0.0 (build 3.0.0-192.0.dev)
        • DevTools version 2.21.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.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
        • Android SDK at /Users/huynq/Library/Android/sdk
        • Platform android-33, build-tools 31.0.0
        • ANDROID_HOME = /Users/huynq/Library/Android/sdk
        • Java binary at: /Applications/Android Studio Dolphin 2021.3.1.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14B47b
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.3)
        • Android Studio at /Applications/Android Studio Dolphin 2021.3.1.app/Contents
        • 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 11.0.13+0-b1751.21-8125866)
    
    [!] Android Studio (version 2022.1)
        • Android Studio at /Applications/Android Studio.app/Contents
        • 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
        ✗ Unable to find bundled Java version.
        • Try updating or re-installing Android Studio.
    
    [✓] IntelliJ IDEA Community Edition (version 2022.1.1)
        • IntelliJ at /Users/huynq/Library/Application Support/JetBrains/Toolbox/apps/IDEA-C/ch-0/221.5591.52/IntelliJ IDEA CE.app
        • 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
    
    [✓] VS Code (version 1.74.3)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.58.0
    
    [✓] Connected device (4 available)
        • RMX2001 (mobile) • EUYTFEUSQSRGDA6D                         • android-arm64  • Android 11 (API 30)
        • iPhone (mobile)  • d9a94afe2b649fef56ba0bfeb052f0f2a7dae95e • ios            • iOS 15.7.2 19H218
        • macOS (desktop)  • macos                                    • darwin-x64     • macOS 13.0.1 22A400 darwin-x64
        • Chrome (web)     • chrome                                   • web-javascript • Google Chrome 109.0.5414.119
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    ! Doctor found issues in 2 categories.
    
  4. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    f: routesNavigator, Router, and related APIs.
    a: error messageError messages from the Flutter framework
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    f: selectionSelectableRegion, SelectionArea, SelectionContainer, Selectable, and related APIs
    c: crashStack traces logged to the console
    and removed
    in triagePresently being triaged by the triage team
    a: error messageError messages from the Flutter framework
    on Feb 2, 2023
  5. added
    P2Important issues not at the top of the work list
    on Feb 7, 2023
  6. asinyagin commented on Oct 24, 2023

    @asinyagin

    I guess I have the same problem. I want to add that my tests fail because of this error. I run my tests on linux.

    Here's my code:

    main.dart
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'SelectableText test',
          home: Navigator(
            pages: const [
              MaterialPage(child: FirstPage()),
              MaterialPage(child: SecondPage()),
            ],
            onPopPage: (route, result) => false,
          ),
        );
      }
    }
    
    class FirstPage extends StatelessWidget {
      const FirstPage({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const Scaffold(
          body: SelectionArea(
            child: Column(children: [
              Text('text1'),
              Text('text2'),
            ]),
          ),
        );
      }
    }
    
    class SecondPage extends StatelessWidget {
      const SecondPage({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: [
              const Text('text on the second page'),
              TextButton(
                onPressed: () => Navigator.pop(context),
                child: const Text('back'),
              ),
            ],
          ),
        );
      }
    }
    integration_test/my_test.dart
    import 'package:flutter_test/flutter_test.dart';
    import 'package:selectabletext_test/main.dart';
    
    void main() {
      testWidgets('the second page', (tester) async {
        await tester.pumpWidget(const MyApp());
        expect(find.text('text on the second page'), findsOneWidget);
      });
    }
    Output from the test
    $ flutter test integration_test/my_test.dart 
    00:00 +0: loading /home/alex/tmp/selectabletext_test/integration_test/my_test.dart                                                                                                                                                                                      B00:12 +0: loading /home/alex/tmp/selectabletext_test/integration_test/my_test.dart                                                                                                                                                                                      
    00:13 +0: the second page                                                                                                                                                                                                                                               
    ══╡ EXCEPTION CAUGHT BY SCHEDULER LIBRARY ╞═════════════════════════════════════════════════════════
    The following assertion was thrown during a scheduler callback:
    'package:flutter/src/rendering/paragraph.dart': Failed assertion: line 931 pos 12:
    '!debugNeedsLayout': 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.yml
    
    When the exception was thrown, this was the stack:
    #2      RenderParagraph.getBoxesForSelection (package:flutter/src/rendering/paragraph.dart:931:12)
    #3      _SelectableFragment._rect (package:flutter/src/rendering/paragraph.dart:1744:45)
    #4      _SelectableFragment.getTransformToParagraph (package:flutter/src/rendering/paragraph.dart:1718:38)
    #5      _SelectableFragment.getTransformTo (package:flutter/src/rendering/paragraph.dart:1723:12)
    #6      MultiSelectableSelectionContainerDelegate._compareScreenOrder (package:flutter/src/widgets/selectable_region.dart:1686:9)
    #7      Sort._insertionSort (dart:_internal/sort.dart:69:36)
    #8      Sort._doSort (dart:_internal/sort.dart:58:7)
    #9      Sort.sort (dart:_internal/sort.dart:33:5)
    #10     ListBase.sort (dart:collection/list.dart:321:10)
    #11     MultiSelectableSelectionContainerDelegate._flushAdditions (package:flutter/src/widgets/selectable_region.dart:1593:70)
    #12     MultiSelectableSelectionContainerDelegate._updateSelectables (package:flutter/src/widgets/selectable_region.dart:1587:7)
    #13     MultiSelectableSelectionContainerDelegate._scheduleSelectableUpdate.runScheduledTask (package:flutter/src/widgets/selectable_region.dart:1569:9)
    #14     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1297:15)
    #15     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1236:9)
    #16     LiveTestWidgetsFlutterBinding.handleDrawFrame (package:flutter_test/src/binding.dart:1747:13)
    #17     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1085:5)
    #21     _invoke (dart:ui/hooks.dart:172:10)
    #22     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:401:5)
    #23     _drawFrame (dart:ui/hooks.dart:140:31)
    (elided 5 frames from class _AssertionError and dart:async)
    ════════════════════════════════════════════════════════════════════════════════════════════════════
    00:13 +0 -1: the second page [E]                                                                                                                                                                                                                                        
      Test failed. See exception logs above.
      The test description was: the second page
      
    
    To run this test again: /home/alex/flutter/bin/cache/dart-sdk/bin/dart test /home/alex/tmp/selectabletext_test/integration_test/my_test.dart -p vm --plain-name 'the second page'
    00:13 +0 -1: Some tests failed.                                                                                                                                                                                                                                         
    
  7. rmahmadkhan commented on Nov 15, 2023

    @rmahmadkhan

    any update on this? i get this a lot on hot reloading on my Flutter web app. v3.13.9

  8. ipapp commented on Feb 16, 2024

    @ipapp

    I have an app with a device list page, from which I open the device details page. I am using go_router, which initializes the background page (devices list page) before the subpage is rendered (details of a specific device).
    If the devices list page contains even a single const Text widget, and I am refreshing the subpage by hot reload or F5, the debugNeedsLayout assert fails.
    A workaround for me was to put the following code into the devices list page build procedure:

    Widget build(BuildContext context) {
        if (ModalRoute.of(context)?.isCurrent == false) {
          return Container();
        }
        // Otherwise build the page as planned.
    }
    

    This is not a solution, just a workaround, but saved me time and log space :-)

  9. moved this from Incoming Issues to Prioritized in SelectableRegion Improvementson Mar 2, 2026
  10. moved this from Updated to Needs Update in SelectableRegion Improvementson Mar 5, 2026
  11. maxgmer commented on Jul 15, 2026

    @maxgmer

    Fix this please!
    The solution by @ipapp may work for some cases, but definitely not others, since the type change forces full widget remounting for the previous page, when it becomes current, which is a heavy redundant work in case of some applications.

  12. moved this from Needs Update to In progress in SelectableRegion Improvementson Jul 19, 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 listc: crashStack traces logged to the consolef: routesNavigator, Router, and related APIs.f: selectionSelectableRegion, SelectionArea, SelectionContainer, Selectable, and related APIsfound in release: 3.7Found to occur in 3.7found in release: 3.8Found to occur in 3.8frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions