Flutter share_plus 插件无法在移动网络上运行

问题描述 投票:0回答:1

我正在使用 Flutter 的 share_plus 插件来尝试共享文本和图像。

当我在 Chrome(网络)模拟器上的 Android Studio 中测试我的应用程序时,此代码有效,它不共享图片,但会打开包含共享主题和文本的系统电子邮件应用程序。但是当我在移动 Chrome 浏览器上运行该应用程序时,共享没有任何作用。我不知道为什么。

我也不知道如何从在网络浏览器中运行应用程序的真实移动设备获取日志。

这是我的代码,我有2个场景,带图片共享和不带图片共享。与网络相关的代码(可能导致失败)是

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"));
flutter web plugins share share-plus
1个回答
0
投票

我在使用 flutter web 时遇到了同样的问题,我用以下方法解决了它

dart:html
套餐。

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');
      });
    }
© www.soinside.com 2019 - 2024. All rights reserved.