Work around the crash by preventing deletes on -1,-1 selection (#73417)
diff --git a/packages/flutter/lib/src/rendering/editable.dart b/packages/flutter/lib/src/rendering/editable.dart
index 3cb0267..4691a9d 100644
--- a/packages/flutter/lib/src/rendering/editable.dart
+++ b/packages/flutter/lib/src/rendering/editable.dart
@@ -825,7 +825,7 @@
final TextSelection selection = textSelectionDelegate.textEditingValue.selection;
final String text = textSelectionDelegate.textEditingValue.text;
assert(_selection != null);
- if (_readOnly) {
+ if (_readOnly || !selection.isValid) {
return;
}
String textBefore = selection.textBefore(text);
diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart
index 6442a4e..27a4f25 100644
--- a/packages/flutter/test/widgets/editable_text_test.dart
+++ b/packages/flutter/test/widgets/editable_text_test.dart
@@ -6917,6 +6917,47 @@
expect(error.toString(), contains(errorText));
});
});
+
+ // Regression test for https://github.com/flutter/flutter/issues/72400.
+ testWidgets("delete doesn't cause crash when selection is -1,-1", (WidgetTester tester) async {
+ final UnsettableController unsettableController = UnsettableController();
+ await tester.pumpWidget(
+ MediaQuery(
+ data: const MediaQueryData(devicePixelRatio: 1.0),
+ child: Directionality(
+ textDirection: TextDirection.ltr,
+ child: EditableText(
+ autofocus: true,
+ controller: unsettableController,
+ backgroundCursorColor: Colors.grey,
+ focusNode: focusNode,
+ style: textStyle,
+ cursorColor: cursorColor,
+ ),
+ ),
+ ),
+ );
+
+ await tester.pump(); // Wait for the autofocus to take effect.
+
+ // Delete
+ await sendKeys(
+ tester,
+ <LogicalKeyboardKey>[
+ LogicalKeyboardKey.delete,
+ ],
+ platform: 'android',
+ );
+
+ expect(tester.takeException(), null);
+ });
+}
+
+class UnsettableController extends TextEditingController {
+ @override
+ set value(TextEditingValue v) {
+ // Do nothing for set, which causes selection to remain as -1, -1.
+ }
}
class MockTextFormatter extends TextInputFormatter {