Skip to content

RangeError when selecting text in SelectionArea #161931

@MrMontazer

Description

@MrMontazer

Steps to reproduce

  1. Select the text "Hello".
  2. Use Shift + Left Arrow to expand the selected text towards the start.
  3. After reaching the beginning of the text, pressing Shift + Left Arrow causes an error (RangeError).
  4. While "Hello" is selected, click on the text in the first line (AssertionError).

Expected results

  • The text selection stops gracefully at the beginning of the text.
  • No errors or unexpected behavior occur when interacting with the text on Line 1.

Actual results

  • Using Shift + Left Arrow beyond the start of the text causes a runtime error.
  • Clicking on the text in the first line triggers an AssertionError.

Code sample

Code sample
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: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: const Center(
        child: SelectionArea(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Directionality(
                textDirection: TextDirection.ltr,
                child: Text('This is the first line.'),
              ),
              Directionality(
                textDirection: TextDirection.ltr,
                child: Text('hello'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Screenshots or Video

Video demonstration
20250121-0755-17.1696126.mp4

Logs

Logs
══╡ EXCEPTION CAUGHT BY SERVICES LIBRARY ╞══════════════════════════════════════════════════════════
The following IndexError was thrown while processing the key message handler:
RangeError (index): Index out of range: index should be less than 1: 1

When the exception was thrown, this was the stack

KeyMessage: KeyMessage([KeyDownEvent#3c2fc(physicalKey: PhysicalKeyboardKey#70050(usbHidUsage:
  "0x00070050", debugName: "Arrow Left"), logicalKey: LogicalKeyboardKey#7a008(keyId: "0x100000302",
  keyLabel: "Arrow Left", debugName: "Arrow Left"), character: null, timeStamp: 0:00:08.077599)])
════════════════════════════════════════════════════════════════════════════════════════════════════
Another exception was thrown: Assertion failed: file:///opt/flutter/packages/flutter/lib/src/widgets/selectable_region.dart:2807:14

Flutter Doctor output

This has been tested on DartPad with Dart SDK 3.6.1 and Flutter SDK 3.27.2 (Stable Channel). However, I also encountered this issue on Windows.

Doctor output
[√] Flutter (Channel master, 3.28.0-2.0.pre.38795, on Microsoft Windows [Version 10.0.22635.4800]) [6.7s]
    • Flutter version 3.28.0-2.0.pre.38795 on channel master
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 79550446df (16 hours ago), 2025-01-20 19:10:34 +0330
    • Engine revision 63110570da
    • Dart version 3.8.0 (build 3.8.0-1.0.dev)
    • DevTools version 2.42.0

[√] Windows Version (11 Enterprise 64-bit, 23H2, 2009) [3.3s]

[√] Android toolchain - develop for Android devices (Android SDK version 33.0.1) [11.7s]
    • Platform android-35, build-tools 33.0.1
    • Java version OpenJDK Runtime Environment (build 17.0.11+0--11852314)
    • All Android licenses accepted.

[√] Chrome - develop for the web [194ms]

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.12.4) [193ms]
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.12.35707.178
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2024.1) [34ms]
    • 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.11+0--11852314)

[√] Connected device (3 available) [219ms]
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22635.4800]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 131.0.6778.265
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 132.0.2957.111

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

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listf: material designflutter/packages/flutter/material repository.f: selectionSelectableRegion, SelectionArea, SelectionContainer, Selectable, and related APIsfound in release: 3.27Found to occur in 3.27found in release: 3.28Found to occur in 3.28frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer versionteam-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions