MissingPluginException(未在通道 plugins.flutter.io/share 上找到方法共享的实现)

MissingPluginException(No implementation found for method share on channel plugins.flutter.io/share)

我在使用 flutter_share: ^1.0.2+1

时发现错误
    MissingPluginException(No implementation found for method share on channel plugins.flutter.io/share)


   E/flutter ( 3361): MissingPluginException(No implementation found for method share on channel plugins.flutter.io/share)
E/flutter ( 3361): #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:278:7)
E/flutter ( 3361): <asynchronous suspension>
E/flutter ( 3361): #1      Share.share (package:share/share.dart:44:20)
E/flutter ( 3361): #2      SignalCard.build.<anonymous closure> 

我通过 remove 我设备上的 flutter 应用程序和 reinstall 通过 USB 或任何其他媒介

来解决它

共享插件按预期工作。出现该错误的原因可能是添加插件后进行了热重启或热重载,而不是完全重启。这意味着插件的平台特定代码未内置到应用程序中。

解决方法:停止应用程序并在添加插件后重新启动它。

来源:https://github.com/flutter/website/pull/1038

如果你在web项目中使用share插件,这个错误会永久出现。 如写 here, you must use share_plus plugin

但是这个插件(share_plus),不能共享文件

  /// Share files
  @override
  Future<void> shareFiles(
    List<String> paths, {
    List<String>? mimeTypes,
    String? subject,
    String? text,
    Rect? sharePositionOrigin,
  }) {
    throw UnimplementedError('shareFiles() has not been implemented on Web.');
  }

并且仅通过邮件分享消息(文本 + 主题)

  // see https://github.com/dart-lang/sdk/issues/43838#issuecomment-823551891
  final uri = Uri(
    scheme: 'mailto',
    query: queryParameters.entries
        .map((e) =>
            '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
        .join('&'),
  );

当您向包含本机代码的应用程序添加插件时,您必须重新启动应用程序才能加载。所以热重启等还不够

注意:flutter_shareshare 软件包已停产。

我现在使用的是官方share_plus插件,你可以在

找到它

https://pub.dev/packages/share_plus

希望这能解决您的问题。谢谢