From fe48140b0803dc6053725ca8563ff19878c4669c Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Tue, 6 Dec 2022 17:45:23 +0530 Subject: [PATCH] Handle error during systemShare --- lib/utils/share_util.dart | 42 +++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/utils/share_util.dart b/lib/utils/share_util.dart index 327ad97bc..037894ac8 100644 --- a/lib/utils/share_util.dart +++ b/lib/utils/share_util.dart @@ -26,23 +26,35 @@ Future share( }) async { final dialog = createProgressDialog(context, "Preparing..."); await dialog.show(); - final List> pathFutures = []; - for (File file in files) { - // Note: We are requesting the origin file for performance reasons on iOS. - // This will eat up storage, which will be reset only when the app restarts. - // We could have cleared the cache had there been a callback to the share API. - pathFutures.add(getFile(file, isOrigin: true).then((file) => file.path)); - if (file.fileType == FileType.livePhoto) { - pathFutures.add(getFile(file, liveVideo: true).then((file) => file.path)); + try { + final List> pathFutures = []; + for (File file in files) { + // Note: We are requesting the origin file for performance reasons on iOS. + // This will eat up storage, which will be reset only when the app restarts. + // We could have cleared the cache had there been a callback to the share API. + pathFutures.add( + getFile(file, isOrigin: true).then((fetchedFile) => fetchedFile.path), + ); + if (file.fileType == FileType.livePhoto) { + pathFutures.add( + getFile(file, liveVideo: true) + .then((fetchedFile) => fetchedFile.path), + ); + } } + final paths = await Future.wait(pathFutures); + await dialog.hide(); + return Share.shareFiles( + paths, + // required for ipad https://github.com/flutter/flutter/issues/47220#issuecomment-608453383 + sharePositionOrigin: shareButtonRect(context, shareButtonKey), + ); + } catch (e, s) { + _logger.severe( + "failed to fetch files for system share ${files.length}", e, s); + await dialog.hide(); + await showGenericErrorDialog(context); } - final paths = await Future.wait(pathFutures); - await dialog.hide(); - return Share.shareFiles( - paths, - // required for ipad https://github.com/flutter/flutter/issues/47220#issuecomment-608453383 - sharePositionOrigin: shareButtonRect(context, shareButtonKey), - ); } Rect shareButtonRect(BuildContext context, GlobalKey shareButtonKey) {