0

I created QR code generating app using flutter-darts. Everything is fine except sharing part. I used following code to share my generated png image.

Future<Null> _captureAndSharePng() async {
    try {
      RenderRepaintBoundary boundary =
          globalKey.currentContext.findRenderObject();
      var image = await boundary.toImage();
      ByteData byteData = await image.toByteData(format: ImageByteFormat.png);
      Uint8List pngBytes = byteData.buffer.asUint8List();

      final tempDir = await getTemporaryDirectory();
      final file = await new File('${tempDir.path}/image.png').create();
      await file.writeAsBytes(pngBytes);

      /*final channel = const MethodChannel('channel:me.alfian.share/share');

        assert(image != null);
        return channel.invokeMethod('shareImage', image);*/
      final channel = const MethodChannel('channel:me.alfian.share/share');
      channel.invokeMethod('shareFile', 'image.png');


    } catch (e) {
      print(e.toString());
    }
  }

When I am trying to share my generated image using above function, an Exception is occuring,

Exception message

What should I do to fix this. I think this will happen because of the channel parameter.

nipun-kavishka
  • 842
  • 12
  • 21
  • MissingPluginException usually occures because you changed your .yaml file, then used the newly added pluging but you did not restart (not hot reload) your application. Make sure to restart your debugging session and it should work. – Jens Mar 11 '20 at 06:15
  • I also restart debugging session, but it did not work. – nipun-kavishka Mar 11 '20 at 06:17

2 Answers2

1

I have done something similar and found out that the Plugin esys_flutter_share: brings the best method to invoke cross paltform sharing.

Try this code snippet, this works for me:

await Share.file(
    'Export', 'export.png', file.readAsBytesSync(), 'export/png');
0

You need to modify MainActivity class also

Follow this answer: https://stackoverflow.com/a/50007287/13617136

iqfareez
  • 555
  • 7
  • 21