pasteText method Null safety

Future<void> pasteText(
  1. SelectionChangedCause cause
)

Paste text from Clipboard.

If there is currently a selection, it will be replaced.

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

Implementation

Future<void> pasteText(SelectionChangedCause cause) async {
  if (_readOnly) {
    return;
  }
  final TextSelection selection = textSelectionDelegate.textEditingValue.selection;
  final String text = textSelectionDelegate.textEditingValue.text;
  assert(selection != null);
  // Snapshot the input before using `await`.
  // See https://github.com/flutter/flutter/issues/11427
  final ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain);
  if (data != null && selection.isValid) {
    _setTextEditingValue(
        TextEditingValue(
          text: selection.textBefore(text) + data.text! + selection.textAfter(text),
          selection: TextSelection.collapsed(
              offset: math.min(selection.start, selection.end) + data.text!.length,
          ),
        ),
        cause,
    );
  }
}