setClipboardData static method

Future<bool> setClipboardData(
  1. Map<String, String> params
)

Writes the specified params to the clipboard asynchronously.

Map<String, String> params = {
  'plainText': 'Hello, world!',
  'htmlText': '<p>Hello, world!</p>',
};
Future<bool> success = ClipboardWeb.setClipboardData(params);
success.then((value) {
  if (value) {
    // Clipboard write operation succeeded
  } else {
    // Clipboard write operation failed or clipboard is unsupported
  }
});

Parameters:

  • params - a Map containing the plain text and HTML text content to write to the clipboard.

Returns:

  • a Future that completes with true if the write operation was successful,
  • a Future that completes with false if the write operation failed or the clipboard is unsupported.

Implementation

static Future<bool> setClipboardData(Map<String, String> params) async {
  final Clipboard clipboard = getClipboard();
  final Map<String, dynamic> representations = <String, html.Blob>{};
  if (params.containsKey('plainText')) {
    /// Write the plain text.
    representations['text/plain'] =
        html.Blob(<dynamic>[params['plainText']], 'text/plain');
  }
  if (params.containsKey('htmlText')) {
    /// Write the HTML text.
    representations['text/html'] =
        html.Blob(<dynamic>[params['htmlText']], 'text/html');
  }
  if (representations.isNotEmpty) {
    /// Write the clipboard content.
    await clipboard.write(
        <ClipboardItem>[ClipboardItem(js_util.jsify(representations))]);
    return true;
  }

  /// Failed to write the clipboard content, return false.
  return false;
}