Sam
Sam

Reputation: 2093

Flutter share_plus plugin not working on mobile web

I'm using Flutter's share_plus plugin to try and share text and images.

When I test my app in Android Studio on the Chrome (web) emulator, this code works, it doesn't share a picture but it opens my system email app with the shared subject and text. But when I run the app on a mobile Chrome browser, sharing does nothing. I'm not sure why.

Nor do I know how to get logs from a real mobile device running an app in its web browser.

This is my code, I have 2 scenarios, sharing with a picture and sharing without. The code that pertains to web, that's presumably causing the failure, is Share.shareXFiles

        GestureDetector(
          onTap: () async {
            //Navigator.of(context).pop();

            String shareStr = "Oi oi oi 👋 Wanna go visit " +
                presentLocation.name +
                "? " +
                presentLocation.why +
                presentLocation.emoji +
                "\n\n" +
                presentLocation.share +
                " 📍";

            // needed for sharing on iPad
            final box = context.findRenderObject() as RenderBox?;

            if (presentLocation.pictureList.isNotEmpty) {

              try {
                var path;
                if(kIsWeb) {
                  path = 'assets/slideshow/${presentLocation.pictureList[0]}';
                } else {
                  final temp = await getTemporaryDirectory();
                  path = '${temp.path}/${presentLocation.pictureList[0]}';
                  var asset =
                      "assets/slideshow/${presentLocation.pictureList[0]}";
                  ByteData imagebyte = await rootBundle.load(asset);
                  File(path)
                      .writeAsBytesSync(imagebyte.buffer.asUint8List());
                }

                await Share.shareXFiles([
                  XFile(path,
                      mimeType:
                          "image/${presentLocation.pictureList[0].split(".")[1]}",
                      name: presentLocation.name)
                ],
                        subject: presentLocation.name +
                            presentLocation.emoji,
                        text: shareStr,
                        sharePositionOrigin:
                            box!.localToGlobal(Offset.zero) & box.size)
                    .then((shareResult) async => {
                          if (shareResult.status ==
                              ShareResultStatus.success)
                            {
                              await MyApp.analytics.logShare(
                                  contentType: "URL shared",
                                  itemId: presentLocation.name,
                                  method: "with image")
                            }
                        });
              } catch (exception) {
                print(
                    "Tried to share with image, hit exception: $exception");
                await Share.shareWithResult(shareStr,
                        subject: presentLocation.name +
                            presentLocation.emoji,
                        sharePositionOrigin:
                            box!.localToGlobal(Offset.zero) & box.size)
                    .then((shareResult) async => {
                          if (shareResult.status ==
                              ShareResultStatus.success)
                            {
                              await MyApp.analytics.logShare(
                                  contentType: "URL shared",
                                  itemId: presentLocation.name,
                                  method: "no image")
                            }
                        });
              }
            } else {
              await Share.shareWithResult(shareStr,
                      subject:
                          presentLocation.name + presentLocation.emoji,
                      sharePositionOrigin:
                          box!.localToGlobal(Offset.zero) & box.size)
                  .then((shareResult) async => {
                        if (shareResult.status ==
                            ShareResultStatus.success)
                          {
                            await MyApp.analytics.logShare(
                                contentType: "URL shared",
                                itemId: presentLocation.name,
                                method: "no image")
                          }
                      });
            }
          },
          child: Text("Button"));

Upvotes: 3

Views: 1384

Answers (1)

Melvin
Melvin

Reputation: 63

I got this same problem when working with flutter web and I solved it with the dart:html package.

import 'dart:html' as html;

void onShare(String text) async {
      final data = {
        'text': text,
      };
      html.window.navigator.share(data).then((_) {
        // Sharing successful
        print('Shared successfully');
      }).catchError((error) {
        // Handle error while sharing
        print('Error sharing: $error');
      });
    }

Upvotes: 3

Related Questions