deleteByLine method Null safety

void deleteByLine(
  1. SelectionChangedCause cause

Deletes a line backwards from the current selection.

If the selection is collapsed, deletes a line before the cursor.

If the selection is not collapsed, deletes the selection.

If obscureText is true, it treats the whole text content as a single word.

The given SelectionChangedCause indicates the cause of this change and will be passed to onSelectionChanged.

See also:


void deleteByLine(SelectionChangedCause cause) {
  assert(_selection != null);

  if (_readOnly || !_selection!.isValid) {

  if (!_selection!.isCollapsed) {
    return _deleteSelection(_selection!, cause);

  // When the text is obscured, the whole thing is treated as one big line.
  if (obscureText) {
    return _deleteToStart(_selection!, cause);

  final String text = textSelectionDelegate.textEditingValue.text;
  String textBefore = _selection!.textBefore(text);
  if (textBefore.isEmpty) {

  // When there is a line break, line delete shouldn't do anything
  final bool isPreviousCharacterBreakLine = textBefore.codeUnitAt(textBefore.length - 1) == 0x0A;
  if (isPreviousCharacterBreakLine) {

  final TextSelection line = _getLineAtOffset(TextPosition(offset: textBefore.length - 1));
  textBefore = textBefore.substring(0, line.start);

  final String textAfter = _selection!.textAfter(text);
  final TextSelection newSelection = TextSelection.collapsed(offset: textBefore.length);
    TextEditingValue(text: textBefore + textAfter, selection: newSelection),