PlatformException(PlatformException(分享回调错误,之前的分享表没有回调,等待吗

问题描述 投票:0回答:3

就我而言,使用以下代码时出现以下错误:

PlatformException (PlatformException(Share callback error, prior share-sheet did not call back, did you await it

final XFile file = XFile(path);
await Share.shareXFiles([file],

但是如果我使用如下所示的已弃用方法(传递列表),则图像共享效果完美:

await Share.shareFiles([path],
subject: subject,
text: text,
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);

shareFiles
被标记为已弃用,但工作正常,在遵循文档并使用
shareXFiles
时,会引发平台异常错误:

我的环境:

Flutter 3.7.10 • 通道稳定 • https://github.com/flutter/flutter.git 框架 • 修订版 4b12645012(9 天前) • 2023-04-03 17:46:48 -0700 发动机 • 修订版 ec975089ac 工具 • Dart 2.19.6 • DevTools 2.20.1

flutter share
3个回答
0
投票

我可以通过将 share_plus 版本从 7.0.2 降级到 7.0.0 来解决此问题。

  final directoryPath = (await getTemporaryDirectory()).path;
  final imagePath = '$directoryPath/qrimg.png';
  final imageFile = await File(imagePath).create(recursive: true);
  imageFile.writeAsBytesSync(byteData.buffer.asUint8List());

  final file = XFile(imagePath);
  await Share.shareXFiles([file]);

Flutter SDK - 3.7.11 安卓版本12


0
投票

shareXFiles 函数似乎仅适用于缓冲区数据。通过 rootBundle.load 加载图像解决了我的问题。

void share(BuildContext context) async {
    final box = context.findRenderObject() as RenderBox?;
    final data = await rootBundle.load('assets/images/example.jpg');
    final buffer = data.buffer;

    await Share.shareXFiles(
      [
        XFile.fromData(
          buffer.asUint8List(data.offsetInBytes, data.lengthInBytes),
          name: 'example.jpg',
          mimeType: 'image/jpeg',
        ),
      ],
      sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
    );
  }

0
投票

步骤1 在 Android studio 中转到:文件 -> 使缓存无效 -> 全部标记并重新启动。

第2步

share_plus:^7.1.0现在分享xFile。

从网址(网络)分享图片

              final dir = await getTemporaryDirectory();
                final Response response = await Dio().get(
                  selected,
                  options: Options(
                    responseType: ResponseType.bytes,
                  ),
                );
                final file = File("${dir.path}/Image.png"); // import 'dart:io';
                 file.writeAsBytesSync(Uint8List.fromList(response.data));
                 debugPrint('==> ${file.path}');

                // Share  
                await Share.shareXFiles(
                  [XFile(file.path)],
                );
              },

从assert/local分享

      onPressed: () async {
                final dir = await getTemporaryDirectory();
                final byte = (await rootBundle.load("assets/images/download.png")).buffer.asUint8List(); // convert in to Uint8List
                debugPrint('byte $byte');
                final file = File("${dir.path}/Image.png") ; // import 'dart:io'
                await file.writeAsBytes(byte);
                // Share
                await Share.shareXFiles(
                  [XFile(file.path)],
                );
              },
© www.soinside.com 2019 - 2024. All rights reserved.