1

I want to let the user copy a file from the device file manager and then paste it into a TextField in my Flutter app.

When the user clicks "Paste" in the text field, I want to get the path or file URI of the file that was copied.

For example: If the user copies /storage/emulated/0/Download/test.pdf, or If they copy a file from a file manager (which may put a content:// URI in clipboard), then when they paste into the text field, I want to automatically get that file path or URI string.

I tried using:

final data = await Clipboard.getData(Clipboard.kTextPlain);
print(data?.text);

But this only works for plain text, not for copied files or URIs (e.g. content://...).

It seems Android and iOS both support copying files (via ClipData / UIPasteboard), but Flutter’s Clipboard API doesn’t expose that.

How can I detect and paste copied files or their paths from the clipboard in Flutter?

4
  • Did you already try reading the file from the provided path? Commented Oct 23 at 0:49
  • I don’t have the file path, Flutter’s clipboard API only gives me text, not the actual file or its URI. I’m looking for a way to get the file or its path when pasting. Commented Oct 23 at 1:18
  • and what does print(data?.text); shows in your case? Commented Oct 23 at 8:26
  • Nothing, just empty or null Commented Oct 24 at 16:51

1 Answer 1

1

You can use the package.

pasteboard: ^0.4.0
import 'package:pasteboard/pasteboard.dart';

TextFormField(
  controller: _controller,
  contextMenuBuilder: (context, editableTextState) {
    return AdaptiveTextSelectionToolbar.buttonItems(
      anchors: editableTextState.contextMenuAnchors,
      buttonItems: <ContextMenuButtonItem>[
        ...editableTextState.contextMenuButtonItems,
        ContextMenuButtonItem(
          onPressed: () async {
            final imageBytes = await Pasteboard.image;
            if (imageBytes == null) return;

            final directory = await getApplicationDocumentsDirectory();
            final imagePath =
                '${directory.path}/pasted_image_${DateTime.now().millisecondsSinceEpoch}.png';
            final file = File(imagePath);
            await file.writeAsBytes(imageBytes);

            _controller.text = imagePath;
            editableTextState.hideToolbar();
          },
          label: 'Paste Image',
        ),
      ],
    );
  },
  onTapOutside: (event) => FocusScope.of(context).unfocus(),
)
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this solution, but it didn’t work, the Pasteboard.files are always empty.
Could you please specify your testing device's specifications?
Simulator iPhone 16 pro, iOS 18.3.1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.