plus_plugins icon indicating copy to clipboard operation
plus_plugins copied to clipboard

[Bug]: shareUri throws an exception when the share dialog is dismissed

Open kbublitz opened this issue 1 year ago • 1 comments

Platform

Android 12

Plugin

share_plus

Version

9.0.0

Flutter SDK

3.19.6

Steps to reproduce

  1. Use shareLink function
  2. Build flutter app for web target
  3. Run the web app on a mobile phone
  4. Trigger the shareLink function by using a button in the app
  5. Dismiss the share drawer by tapping outside of it

Code Sample

final shareLink = Uri.parse("www.github.com");    
final box = context.findRenderObject() as RenderBox?;
// shareUri seems to work better than share, but does not allow to set a subject for mail
final shareResult = await Share.shareUri(
  shareLink,
  sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
);

Logs

Can't run flutter run --verbose on a deployed web app, I see the following error message:

Exception: Navigator.share() failed: The operation was aborted

Flutter Doctor

$ flutter doctor -v
[√] Flutter (Channel stable, 3.19.6, on Microsoft Windows [Version 10.0.19045.4291], locale de-DE)
    • Flutter version 3.19.6 on channel stable at C:\Users\kbublitz\Tools\flutter   
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 54e66469a9 (3 weeks ago), 2024-04-17 13:08:03 -0700        
    • Engine revision c4cd48e186
    • Dart version 3.3.4
    • DevTools version 2.31.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] VS Code (version 1.90.0-insider)
    • VS Code at C:\Users\kbublitz\AppData\Local\Programs\Microsoft VS Code Insiders
    • Flutter extension version 3.89.20240501

[√] Connected device (2 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 124.0.6367.119
    • Edge (web)   • edge   • web-javascript • Microsoft Edge 124.0.2478.67

[√] Network resources
    • All expected network resources are available.

• No issues found!

Checklist before submitting a bug

  • [X] I searched issues in this repository and couldn't find such bug/problem
  • [X] I Google'd a solution and I couldn't find it
  • [X] I searched on StackOverflow for a solution and I couldn't find it
  • [X] I read the README.md file of the plugin
  • [X] I'm using the latest version of the plugin
  • [X] All dependencies are up to date with flutter pub upgrade
  • [X] I did a flutter clean
  • [X] I tried running the example project

kbublitz avatar May 07 '24 09:05 kbublitz

My guess is that the following lines from share() should also be added to the shareUri method in the share_plus_web.dart:

if (e.name case 'AbortError') { return _resultDismissed; }

kbublitz avatar May 07 '24 10:05 kbublitz

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days

github-actions[bot] avatar Aug 06 '24 00:08 github-actions[bot]

Commenting to prevent auto closing. Would be nice to get any feedback if this issue will be fixed.

kbublitz avatar Aug 12 '24 06:08 kbublitz

Is this an android error or a web error? your report says Android 12, but the logs talk about deploying for web

miquelbeltran avatar Aug 12 '24 06:08 miquelbeltran

Yes it's the web target, but the error is still specific to Android, so I can't debug or test it in a web browser on PC. I wasn't sure what the template was asking for, the App is running in a web browser on an Android device when the exception appears.

kbublitz avatar Aug 12 '24 06:08 kbublitz

Are you wrapping the call with a try/catch? The method throws exceptions if it fails: https://github.com/fluttercommunity/plus_plugins/blob/e61b4725332adfca756cd77c97354f2f29be0907/packages/share_plus/share_plus/lib/src/share_plus_web.dart#L32-L68

miquelbeltran avatar Aug 12 '24 07:08 miquelbeltran

Sure we catch the exception and show an error popup (something generic like "Failed to share the URI"). It's just that I don't think there should be an exception in case of aborting the dialog, just to be consistent with the behavior in share() method.

kbublitz avatar Aug 12 '24 10:08 kbublitz

My guess is that the difference between share() and shareUri is that the former has the following code in the try/catch:

      if (e.name case 'AbortError') {
        return _resultDismissed;
      }

Which the shareUri doesn't have.

This should be an easy fix.

miquelbeltran avatar Aug 12 '24 11:08 miquelbeltran