deleteForwardByWord method Null safety

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

Deletes a word in the forward direction from the current selection.

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

  • deleteByWord, which is same but in the opposite direction.


void deleteForwardByWord(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 word.
  if (obscureText) {
    return _deleteToEnd(_selection!, cause);

  final String text = textSelectionDelegate.textEditingValue.text;
  String textAfter = _selection!.textAfter(text);

  if (textAfter.isEmpty) {

  final String textBefore = _selection!.textBefore(text);
  final int characterBoundary = _getRightByWord(_textPainter, textBefore.length, includeWhitespace);
  textAfter = textAfter.substring(characterBoundary - textBefore.length);

    TextEditingValue(text: textBefore + textAfter, selection: _selection!),