首页 > 解决方案 > 无法使用 Flutter 通道共享文件 - 缺少插件异常

问题描述

我使用flutter-darts创建了QR 码生成应用程序。一切都很好,除了分享部分。我使用以下代码来分享我生成的png 图像

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());
    }
  }

当我尝试使用上述功能共享我生成的图像时,发生异常,

异常消息

我应该怎么做才能解决这个问题。我认为这会因为通道参数而发生。

标签: imageflutterdartshareflutter-plugin

解决方案


我做过类似的事情,发现插件 esys_flutter_share:带来了调用跨平台共享的最佳方法。

试试这个代码片段,这对我有用:

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

推荐阅读