deleteByWord method Null safety

void deleteByWord(
  1. SelectionChangedCause cause,
  2. [bool includeWhitespace = true]

Deletes a word backwards from the current selection.

If the selection is collapsed, deletes a word 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.

By default, includeWhitespace is set to true, meaning that whitespace can be considered a word in itself. If set to false, the selection will be extended past any whitespace and the first word following the whitespace.

See also:


void deleteByWord(SelectionChangedCause cause, [bool includeWhitespace = true]) {
  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) {

  final int characterBoundary = _getLeftByWord(_textPainter, textBefore.length, includeWhitespace);
  textBefore = textBefore.trimRight().substring(0, characterBoundary);

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