Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter can't read from Clipboard

I come asking for quite a specific question regarding Flutter and the Future and await mechanism, which seems to be working, but my Clipboard does not really function while operating with my editable text fields, even following Google's advice on implementation...

This is my code for pasting:

onPressed: () async {
  await getMyData('text');
  _encodingController.text = clipData;
  Scaffold.of(context).showSnackBar(
    new SnackBar(
      content: new Text(
        "Pasted from Clipboard"),
      ),
    );
},

what doesnt work is my paste functionality... While debugging the result of this following function is null, wth?????????

static Future<ClipboardData> getMyData(String format) async {
    final Map<String, dynamic> result =
        await SystemChannels.platform.invokeMethod(
      'Clipboard.getData',
      format,
    );

    if (result == null) {
      return null;
    } else {
      clipData = ClipboardData(text: result['text']).text;
      return ClipboardData(text: result['text'].text);
    }
  }

I am probably using the Futures and async await wrong, would love some guidance!!! Copying is working using the Clipboard Manager plugin! Thanks very much!

like image 200
Josep Jesus Bigorra Algaba Avatar asked Nov 20 '18 12:11

Josep Jesus Bigorra Algaba


People also ask

How do you get text from clipboard in flutter?

In the onPressed parameter of the TextButton , call the getData method from the Clipboard class. We need to pass in the format, so in our case, use text/plain — the format when retrieving texts from the clipboard.


2 Answers

You can simply re-use Flutter's existing library code to getData from Clipboard.

ClipboardData data = await Clipboard.getData('text/plain');
like image 187
noam aghai Avatar answered Nov 01 '22 03:11

noam aghai


First create a method

Future<String> getClipBoardData() async {
        ClipboardData data = await Clipboard.getData(Clipboard.kTextPlain);
        return data.text;
      }

Then in build method

FutureBuilder(
                future: getClipBoardData(),
                initialData: 'nothing',
                builder: (context, snapShot){
                  return Text(snapShot.data.toString());
                },
              ),
like image 4
Gagan Raghunath Avatar answered Nov 01 '22 03:11

Gagan Raghunath