HarmonyOS鸿蒙Next中flutter开发 url_launcher无法打开网页,以及open_filex无法调用其他app打开文件

HarmonyOS鸿蒙Next中flutter开发 url_launcher无法打开网页,以及open_filex无法调用其他app打开文件 鸿蒙flutter版本:

cke_999.png

鸿蒙flutter壳工程:

依赖覆盖

dependency_overrides:
  shared_preferences:
    git:
      url: "https://gitee.com/openharmony-sig/flutter_packages.git"
      path: "packages/shared_preferences/shared_preferences"

  connectivity_plus:
    git:
      url: "https://gitee.com/openharmony-sig/flutter_plus_plugins.git"
      path: "packages/connectivity_plus/connectivity_plus"

  package_info_plus:
    git:
      url: "https://gitee.com/openharmony-sig/flutter_plus_plugins.git"
      path: "packages/package_info_plus/package_info_plus"

  path_provider:
      git:
        url: "https://gitee.com/openharmony-sig/flutter_packages.git"
        path: "packages/path_provider/path_provider"

  sqflite:
    git:
      url: "https://gitcode.com/openharmony-sig/flutter_sqflite.git"
      path: ./sqflite
      ref: br_v2.3.3+1_ohos

  url_launcher:
    git:
      url: "https://gitcode.com/openharmony-tpc/flutter_packages.git"
      path: "packages/url_launcher/url_launcher"
      ref: br_url_launcher-v6.3.0_ohos

通过url打开网页代码

final Uri uri = Uri.parse(url);
if (!await launchUrl(uri)) {
  throw Exception('Could not launch $url');
}

通过url_launcher调用其他app打开文件代码

Future<void> openPdfWithUrlLauncher(BuildContext context, File pdfFile, Item targetPdfAttachmentItem) async {
  try {
    final result = await OpenFilex.open(
      pdfFile.path,
      type: "application/pdf",
    );

    MyLogger.d('打开PDF结果: ${result.type} - ${result.message}');

    switch (result.type) {
      case ResultType.done:
        MyLogger.d('已打开 ${targetPdfAttachmentItem.getTitle()}');
        zoteroDB.addRecentlyOpenedAttachments(targetPdfAttachmentItem);
        break;
      case ResultType.noAppToOpen:
        BrnToast.show('未找到可以打开PDF的应用,请安装PDF阅读器', context);
        break;
      case ResultType.fileNotFound:
        BrnToast.show('文件不存在', context);
        break;
      case ResultType.permissionDenied:
        BrnToast.show('没有权限打开文件', context);
        break;
      case ResultType.error:
        BrnToast.show('打开失败: ${result.message}', context);
        break;
    }
  } catch (e) {
    MyLogger.e('打开PDF失败: $e');
    BrnToast.show('打开PDF失败: $e', context);
  }
}

相关错误日志:

 com.moyear.zotpaper   W     flutter settings log message: Moyear=== viewItemOnline: https://kns.cnki.net/kcms2/article/abstract?v=VcTOyLYtvExLp40xgGTg_i-auTdOQlnySEio7-JWzIibdeqwEjK_Fi4Do3hH-_d4fbwVPb3ZRsfX85WsqcMUk3DNVL1XNc2FoF44S7TACjR_sedIBva22YrAIz19B4ThNLwanqdRjY7-DPu3kB_w1PLLtUKWLj_TdPD5qmeYJNVwMblhiAWlfwHAZt5A2xQZ&uniplatform=NZKPT&language=CHS
11-06 16:57:59.992   3010-3010     A00000/XComFlutterEngine        com.moyear.zotpaper   I     Thread:548301703280  [INFO:flutter/shell/platform/ohos/napi/platform_view_ohos_napi.cpp(298)] FlutterViewHandlePlatformMessage message channal plugins.flutter.io/url_launcher
11-06 16:57:59.992   3010-3010     A00000/XComFlutterEngine        com.moyear.zotpaper   I     Thread:548301703280  [INFO:flutter/shell/platform/ohos/napi/platform_view_ohos_napi.cpp(329)] FlutterViewHandlePlatformMessage mapData= 
11-06 16:57:59.992   3010-3010     A000ff/Flutter                  com.moyear.zotpaper   D     FlutterNapi --> called handlePlatformMessage Channel: plugins.flutter.io/url_launcher. Response ID: 240
11-06 16:57:59.992   3010-3010     A000ff/Flutter                  com.moyear.zotpaper   D     DartMessenger --> Received message from Dart over channel 'plugins.flutter.io/url_launcher'
11-06 16:57:59.992   3010-3010     A000ff/Flutter                  com.moyear.zotpaper   D     DartMessenger --> No registered handler for message. Responding to Dart with empty reply message.
11-06 16:57:59.992   3010-3010     A00000/XComFlutterEngine        com.moyear.zotpaper   I     Thread:548301703280  [INFO:flutter/shell/platform/ohos/napi/platform_view_ohos_napi.cpp(169)] nativeInvokePlatformMessageEmptyResponseCallback
11-06 16:57:59.992   3010-3010     A00000/XComFlutterEngine        com.moyear.zotpaper   I     Thread:548301703280  [INFO:flutter/shell/platform/ohos/napi/platform_view_ohos_napi.cpp(187)] InvokePlatformMessageEmptyResponseCallback
11-06 16:58:00.011   3010-3010     A00000/XComFlutterEngine        com.moyear.zotpaper   I     Thread:548301703280  [INFO:flutter/shell/platform/ohos/napi/platform_view_ohos_napi.cpp(298)] FlutterViewHandlePlatformMessage message channal flutter/mousecursor
11-06 16:58:00.011   3010-3010     A00000/XComFlutterEngine        com.moyear.zotpaper   I     Thread:548301703280  [INFO:flutter/shell/platform/ohos/napi/platform_view_ohos_napi.cpp(329)] FlutterViewHandlePlatformMessage mapData= 
11-06 16:58:00.011   3010-3010     A000ff/Flutter                  com.moyear.zotpaper   D     FlutterNapi --> called handlePlatformMessage Channel: flutter/mousecursor. Response ID: 241
11-06 16:58:00.011   3010-3010     A000ff/Flutter                  com.moyear.zotpaper   D     DartMessenger --> Received message from Dart over channel 'flutter/mousecursor'
11-06 16:58:00.011   3010-3010     A000ff/Flutter                  com.moyear.zotpaper   D     DartMessenger --> Deferring to

更多关于HarmonyOS鸿蒙Next中flutter开发 url_launcher无法打开网页,以及open_filex无法调用其他app打开文件的实战教程也可以访问 https://www.itying.com/category-92-b0.html

3 回复

您好,

日志里面提示:No registered handler for message. Responding to Dart with empty reply message;

大概率是您具体实现不正确;

请您根据以上两个库的官方demo排查下两个库的具体用法,

https://gitcode.com/openharmony-tpc/flutter_packages/tree/br_url_launcher-v6.3.0_ohos/packages/url_launcher/url_launcher_ohos/example

https://gitcode.com/openharmony-sig/fluttertpc_open_filex/tree/master/example

demo运行指导:用DevEco Studio打开ohos文件夹->本地自动签名->在example目录下执行:flutter build hap

更多关于HarmonyOS鸿蒙Next中flutter开发 url_launcher无法打开网页,以及open_filex无法调用其他app打开文件的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在HarmonyOS Next中,Flutter插件url_launcher和open_filex可能因系统权限或API适配问题导致功能异常。需检查插件是否已适配HarmonyOS Next的API,并确保在配置文件中声明必要的网络和文件访问权限。部分插件可能需要针对HarmonyOS进行特定适配或使用替代方案。

在HarmonyOS Next的Flutter开发中,url_launcher无法打开网页以及open_filex无法调用其他应用打开文件,通常是由于平台通道(Platform Channel)未正确注册或插件未适配鸿蒙系统导致的。

根据您提供的日志,关键错误信息是:

No registered handler for message. Responding to Dart with empty reply message.

这表明Dart端通过plugins.flutter.io/url_launcher通道发送了消息,但原生侧(HarmonyOS)没有对应的处理器(handler)来接收和处理该消息。

问题分析:

  1. url_launcher 插件未正确集成或未适配鸿蒙: 您通过dependency_overrides指定了鸿蒙版本的url_launcher插件(br_url_launcher-v6.3.0_ohos)。此版本可能未完全实现HarmonyOS Next所需的原生侧代码,或者其原生侧代码(Java/ArkTS)未在您的HarmonyOS工程中正确注册。

  2. open_filex 插件问题: 您的代码中使用了OpenFilex.open,但未在pubspec.yamldependency_overrides中看到对该插件的依赖声明和鸿蒙版本覆盖。该插件很可能未进行鸿蒙适配,导致其平台通道在HarmonyOS侧无法工作。

解决方案:

  1. 确认并验证插件鸿蒙版本:

    • 确保您使用的url_launcher鸿蒙分支(br_url_launcher-v6.3.0_ohos)是官方维护且支持您当前HarmonyOS Next SDK版本的。
    • 对于open_filex,您需要寻找其专为鸿蒙(OpenHarmony/HarmonyOS)移植的版本。可以访问OpenHarmony SIG仓华为开发者联盟插件市场搜索是否有适配版本。如果没有,则需要考虑使用其他已适配的类似插件(如file_picker的鸿蒙版本)或自行进行插件鸿蒙化改造。
  2. 检查HarmonyOS侧原生代码注册:

    • Flutter插件在HarmonyOS侧需要注册其平台通道实现。请检查您的HarmonyOS工程(entry/src/main/ets/目录下)是否包含了所用插件的原生代码包,并确保在应用启动时这些插件的原生代码被正确初始化。通常,鸿蒙化插件会提供详细的集成文档。
  3. 检查权限配置:

    • 在HarmonyOS应用中,打开网页或调用其他应用可能需要声明相应的权限。请检查您的HarmonyOS应用配置文件module.json5,确保已添加必要的权限,例如网络权限、以及启动其他应用所需的权限(如ohos.permission.START_ABILITIES_FROM_BACKGROUND)。
  4. 替代方案测试:

    • 对于打开网页,可以暂时尝试使用鸿蒙原生的Web组件或通过UIAbility拉起系统浏览器来验证是否是url_launcher插件本身的问题。
    • 对于打开文件,可以尝试使用HarmonyOS的Want机制直接启动其他应用来打开文件,以验证文件路径和系统交互是否正常。

总结: 当前问题的核心是Flutter插件的鸿蒙平台层实现缺失或未正确集成。请重点检查插件的鸿蒙兼容性、原生代码集成以及HarmonyOS工程配置。

回到顶部