handlePaste method Null safety

Future<void> handlePaste(
  1. TextSelectionDelegate delegate
)

Paste the current clipboard selection (obtained from Clipboard) into the text field managed by the given delegate, replacing its current selection, if any. Then, hide the toolbar.

This is called by subclasses when their paste affordance is activated by the user.

This function is asynchronous since interacting with the clipboard is asynchronous. Race conditions may exist with this API as currently implemented.

Implementation

// TODO(ianh): https://github.com/flutter/flutter/issues/11427
Future<void> handlePaste(TextSelectionDelegate delegate) async {
  final TextEditingValue value = delegate.textEditingValue; // Snapshot the input before using `await`.
  final ClipboardData? data = await Clipboard.getData(Clipboard.kTextPlain);
  if (data != null) {
    delegate.userUpdateTextEditingValue(
      TextEditingValue(
        text: value.selection.textBefore(value.text)
            + data.text!
            + value.selection.textAfter(value.text),
        selection: TextSelection.collapsed(
            offset: value.selection.start + data.text!.length,
        ),
      ),
      SelectionChangedCause.toolBar,
    );
  }
  delegate.bringIntoView(delegate.textEditingValue.selection.extent);
  delegate.hideToolbar();
}