[Bug]: Share not working on web
Platform
Web (Chrome(Mac), Safari(Mac), Mobile Safari/Chrome)
Plugin
share_plus
Version
6.2.0
Flutter SDK
3.3.7
Steps to reproduce
- open the app in the chrome
- tab on the button with below code snippet
Code Sample
ElevatedButton(onPressed: () async {
var data = await http.get(Uri.parse("https://cdn.pixabay.com/photo/2016/02/28/12/55/boy-1226964_1280.jpg"));
XFile xfile = XFile.fromData(data.bodyBytes, mimeType: "image/jpeg");
await Share.shareXFiles([xfile]);
}, child: const Text("Share Photo"),)
add this to you widget tree, and tap on this button to share
Instruction share_plus readme, are ambiguous for web there is no way to have a code like
"On web you can use SharePlus.shareXFiles() "
Logs
Waiting for connection from debug service on Chrome...
Restarted application in 91ms.
TypeError: this.share is not a function
dart-sdk/lib/html/dart2js/html_dart2js.dart 22913:49 share]
packages/share_plus/src/share_plus_web.dart 103:22 shareXFiles
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50 <fn>
dart-sdk/lib/async/zone.dart 1653:54 runUnary
dart-sdk/lib/async/future_impl.dart 147:18 handleValue
dart-sdk/lib/async/future_impl.dart 766:44 handleValueCallback
dart-sdk/lib/async/future_impl.dart 795:13 _propagateToListeners
dart-sdk/lib/async/future_impl.dart 557:7 [_complete]
dart-sdk/lib/async/stream_pipe.dart 61:11 _cancelAndValue
dart-sdk/lib/async/stream.dart 1587:7 <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14 _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39 dcall
dart-sdk/lib/html/dart2js/html_dart2js.dart 37341:58
Flutter Doctor
[✓] Flutter (Channel stable, 3.3.7, on macOS 13.1 22C5033e darwin-arm, locale en-IN)
[!] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
✗ cmdline-tools component is missing
Run `path/to/sdkmanager --install "cmdline-tools;latest"`
See https://developer.android.com/studio/command-line for more details.
✗ Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
[✓] Chrome - develop for the web
[!] Android Studio
✗ Unable to find bundled Java version.
[✓] Android Studio (version 2021.2)
[!] Android Studio
✗ Unable to find bundled Java version.
[✓] IntelliJ IDEA Ultimate Edition (version 2022.2.3)
[✓] VS Code (version 1.73.0)
[✓] Connected device (3 available)
[✓] HTTP Host Availability
Checklist before submitting a bug
- [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
I guess we should add this to the package README:
/// Share [XFile] objects.
///
/// Remarks for the web implementation:
/// This uses the [Web Share API](https://web.dev/web-share/) if it's
/// available. Otherwise, uncaught Errors will be thrown.
/// See [Can I Use - Web Share API](https://caniuse.com/web-share) to
/// understand which browsers are supported. This builds on the
/// [`cross_file`](https://pub.dev/packages/cross_file) package.
So, according to https://caniuse.com/web-share, is your browser supported?
According to that site, Share on web is only supported on Chrome on Windows, and you are on a Mac.
Leaving this open because this needs to be documented in the package README.md
Oh, But I see safari as supported browser, but it doesn't work there as well.
Also hosted it locally, and tried to access the same on mobile browsers, safari on iOS, chrome on android, doesn't work on those as well
Oh, But I see safari as supported browser, but it doesn't work there as well.
That would be a different issue then. Can you paste logs and console logs from the browser here when running on Safari?
I tried to reproduce on Safari, but I am not able to set up a dev environment there, so I am waiting for your logs.
same here, the logs are not available from safari, maybe because of the unavailability of dev tools, but does tapping on share works on safari instance of yours?
When I run the site there, I saw an error in the Safari console that didn't seem related to the plugin. You can enable console logs on it, but I'd like to see debug logs from a supported browser/OS to know exactly that the problem is.
So I tried getting logs from terminal, no logs maybe because of debug services are only available on Chrome, and on safari console, whenever I tap on the said button to share an xFile, it just gives this.

here are browser logs from Edge and Chrome on windows which is supported as per https://caniuse.com/web-share

Sharing on web must be initiated via click, see https://developer.mozilla.org/en-US/docs/Web/API/Navigator/share#security
It also has a couple of other limitations as mentioned in https://w3c.github.io/web-share/#share-method
Can you elaborate @ueman, does it mean that the native web share method cannot be called from Flutter?
Also ping to @ThexXTURBOXx who implemented the web share
Can you elaborate @ueman, does it mean that the native web share method cannot be called from Flutter?
It can, but it can't be called without any user interaction. The links do explain it better than I can, though.
I just thought it would be nice to have that mentioned in the docs somewhere, because it's kinda quirky compared to what we know from Android and iOS.
I tested the web share on Edge and Chrome on Windows - in addition to some other ones. The library worked on both of them back then. However, there have been some updates since then, maybe one of them broke it? I will test functionality again in a few minutes.
The example implementation has been broken since https://github.com/fluttercommunity/plus_plugins/commit/e1d3553b69a0156701613b2fc1cbb9cad1e3fa0c
After fixing it, I tested it again on Chrome, Edge, and Firefox.
As alreaddy stated before, sharing files on Firefox is not supported in any way. However, sharing simple text is still working.
Chrome and Edge are still working completely fine when sharing text and/or files.
I am using latest main branch share_plus and Flutter 3.3.8 in the example web (with a tiny fix in the example which I will PR soon).
Thus, it must be something on your end. Please try out the example and see if that one also does not work for you (after I fixed it).
Firefox:

Chrome:

Edge:

After fixing it, I tested it again on Chrome, Edge, and Firefox.
Can you submit a PR with the example fix? I'd like to understand what broke it.
@miquelbeltran PR has been submitted! :)
Thanks! I see that the issue with the example is unrelated to the problem @carotkut94 has, but at least they can use now the example to check if it works at all.
@ueman if you see the the code which I proposed, it has a click action attached to a button, also., the sample app from the source, also doesn't work on the said browsers, and fails with the same error. the error which you see in the browser, are coming only when I am tapping in the button.