
How to paste from device clipboard in Flutter.
Hey flutter friends, I’d like to show you the simple act of pasting from the clipboard. Yes! Pasting. “Oh! don’t I already know that?” well, maybe you do but here’s why am I writing about this.
Reason for writing
I have built quite a few apps that enabled users the ability to tap an Iconbutton or a button and copy a text and it worked all fine until I was building a URL shortener. This time, I needed the user to tap an IconButton and paste whatever was in their clipboard, and to my greatest surprise, the plugin I was using didn’t have the ability to paste.
I did a little digging and found another plugin that would paste but it will only paste what was copied in your app which wasn’t what I wanted, remember I wanted to paste what was in the device clipboard, so if a user copied something from maybe Whatsapp, twitter, etc. if he tapped the paste icon in my app, it should paste what's in his clipboard in my app right.
So I found another package that enabled me to do just this. It allows copying and pasting. Here’s all you need to achieve this.
First visit pub.dev/packages/clipboard
Paste this in your pubspec.yaml file:
clipboard: ^0.1.2+5
then run:
flutter pub get
Now make sure to import into your dart file.
import 'package:clipboard/clipboard.dart';
Here’s a code snippet of how to Paste from Clipboard:
Here’s a code snippet of how to Copy to Clipboard:
That’s it for this article! I hope you enjoyed it, and be sure to follow me for more Flutter articles and comment for any feedback you might have about this article.