英文:
PlatformException (PlatformException(Share callback error, prior share-sheet did not call back, did you await it
问题
在我的情况下,使用以下代码出现以下错误:当使用以下代码时出现PlatformException (PlatformException(分享回调错误,之前的分享表格没有回调,你是否等待它
:
final XFile file = XFile(path);
await Share.shareXFiles(,
但如果我像下面这样使用已弃用的方法(传递List),分享图片就可以正常工作:
await Share.shareFiles([path],
subject: subject,
text: text,
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
尽管shareFiles
被标记为已弃用,但它仍然可以正常工作,而遵循文档并使用shareXFiles
会引发平台异常错误:
我的环境:
Flutter 3.7.10 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 4b12645012(9天前) • 2023年4月3日 17:46:48 -0700
引擎 • revision ec975089ac
工具 • Dart 2.19.6 • DevTools 2.20.1
英文:
In my case am getting the following error: PlatformException (PlatformException(Share callback error, prior share-sheet did not call back, did you await it
when using below code:
final XFile file = XFile(path);
await Share.shareXFiles(,
but if I use the deprecated method like below (passing the List), the sharing of image works perfectly:
await Share.shareFiles([path],
subject: subject,
text: text,
sharePositionOrigin: box.localToGlobal(Offset.zero) & box.size);
shareFiles
is marked as deprecated but works fine, while following documentation and using shareXFiles
, throws the plaform exception error:
My environment:
Flutter 3.7.10 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 4b12645012 (9 days ago) • 2023-04-03 17:46:48 -0700
Engine • revision ec975089ac
Tools • Dart 2.19.6 • DevTools 2.20.1
答案1
得分: 2
步骤 1
在Android Studio中,转到:文件 -> 使缓存失效 -> 全部标记并重新启动。
步骤 2
share_plus: ^7.1.0 现在可以分享 xFile。
从 URL(网络)分享图像
final dir = await getTemporaryDirectory();
final Response response = await Dio().get(
selected,
options: Options(
responseType: ResponseType.bytes,
),
);
final file = File("${dir.path}/Image.png"); // 导入 'dart:io';
file.writeAsBytesSync(Uint8List.fromList(response.data));
debugPrint('==》 ${file.path}');
// 分享
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(); // 转换成 Uint8List
debugPrint('byte $byte');
final file = File("${dir.path}/Image.png") ; // 导入 'dart:io';
await file.writeAsBytes(byte);
// 分享
await Share.shareXFiles(
[XFile(file.path)],
);
},
英文:
Step 1
in Android studio goto : File -> Invalidate Caches -> mark all and restart.
Step 2
share_plus: ^7.1.0 now share xFile.
Share image from Url (Network)
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)],
);
},
Share from 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)],
);
},
答案2
得分: 1
我刚刚执行了 flutter clean
然后 flutter pub get
或者直接运行。
英文:
I just did flutter clean
and then flutter pub get
or just run.
答案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();
Flutter SDK - 3.7.11
Android 版本 12
英文:
I was able to resolve this issue by downgrading the share_plus version from 7.0.2 to 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();
Flutter SDK - 3.7.11
Android version 12
答案4
得分: 0
The shareXFiles
function似乎仅使用缓冲数据运行。通过使用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,
);
}
英文:
shareXFiles function seems to be working with buffer data only. Loading images via rootBundle.load fixes the problem in my case.
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,
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论