HarmonyOS鸿蒙Next中使用flutter 3.22.1-ohos-1.0.4版本开发,目前flutter build hap后在DevEco Studio上启动项目报错
HarmonyOS鸿蒙Next中使用flutter 3.22.1-ohos-1.0.4版本开发,目前flutter build hap后在DevEco Studio上启动项目报错 使用的是flutter 3.22.1-ohos-1.0.4 这个版本进行开发的,目前flutter build hap 后在DevEco-Studio上启动项目 报这些错误:
-
ERROR: ArkTS:ERROR File: /Users/rock/flutterProjects/hxc/ohos/oh_modules/.ohpm/@ohos+flutter_ohos@z+esrfzju6x1o3bcfuku8xtvaawms8gp1tiq996zf1o=/oh_modules/@ohos/flutter_ohos/src/main/ets/plugin/editing/TextInputPlugin.ets:236:36 Namespace ‘inputMethod’ has no exported member ‘RequestKeyboardReason’.
-
ERROR: ArkTS:ERROR File: /Users/rock/flutterProjects/hxc/ohos/oh_modules/.ohpm/@ohos+flutter_ohos@z+esrfzju6x1o3bcfuku8xtvaawms8gp1tiq996zf1o=/oh_modules/@ohos/flutter_ohos/src/main/ets/plugin/editing/TextInputPlugin.ets:241:28 Property ‘RequestKeyboardReason’ does not exist on type ‘typeof inputMethod’.
-
ERROR: ArkTS:ERROR File: /Users/rock/flutterProjects/hxc/ohos/oh_modules/.ohpm/@ohos+flutter_ohos@z+esrfzju6x1o3bcfuku8xtvaawms8gp1tiq996zf1o=/oh_modules/@ohos/flutter_ohos/src/main/ets/plugin/editing/TextInputPlugin.ets:243:28 Property ‘RequestKeyboardReason’ does not exist on type ‘typeof inputMethod’.
-
ERROR: ArkTS:ERROR File: /Users/rock/flutterProjects/hxc/ohos/oh_modules/.ohpm/@ohos+flutter_ohos@z+esrfzju6x1o3bcfuku8xtvaawms8gp1tiq996zf1o=/oh_modules/@ohos/flutter_ohos/src/main/ets/plugin/editing/TextInputPlugin.ets:247:28 Property ‘RequestKeyboardReason’ does not exist on type ‘typeof inputMethod’.
-
ERROR: ArkTS:ERROR File: /Users/rock/flutterProjects/hxc/ohos/oh_modules/.ohpm/@ohos+flutter_ohos@z+esrfzju6x1o3bcfuku8xtvaawms8gp1tiq996zf1o=/oh_modules/@ohos/flutter_ohos/src/main/ets/plugin/editing/TextInputPlugin.ets:249:28 Property ‘RequestKeyboardReason’ does not exist on type ‘typeof inputMethod’.
使用鸿蒙SDK:5.0.2
更多关于HarmonyOS鸿蒙Next中使用flutter 3.22.1-ohos-1.0.4版本开发,目前flutter build hap后在DevEco Studio上启动项目报错的实战教程也可以访问 https://www.itying.com/category-92-b0.html
- 看报错信息是由于TextInputPlugin.ets文件中的,使用的inputMethod模块中找不到RequestKeyboardReason成员。建议使用最新的稳定版本Flutter 3.22.0-ohos 1.0.4 Release以及版本以及配置对应的配套工具以及SDK:https://gitcode.com/openharmony-tpc/flutter_flutter/blob/3.22.0-ohos/release-notes/Flutter%203.22.0-ohos%201.0.4%20ReleaseNote.md#%E7%89%88%E6%9C%AC%E9%85%8D%E5%A5%97
- 5.0.2对应的API版本是14,而RequestKeyboardReason是从API 15之后才支持,具体可参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-inputmethod#requestkeyboardreason15
- 需要根据这个文档去下载并安装最新DevEco Studio开发工具、SDK,及其依赖环境:https://gitcode.com/openharmony-tpc/flutter_samples/blob/master/ohos/docs/03_environment/openHarmony-flutter%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA%E6%8C%87%E5%AF%BC.md#111-%E5%AE%98%E6%96%B9%E4%B8%8B%E8%BD%BD%E5%9C%B0%E5%9D%80 ,保证SDK要升级到15及以上才能使用这个inputMethod.RequestKeyboardReason
更多关于HarmonyOS鸿蒙Next中使用flutter 3.22.1-ohos-1.0.4版本开发,目前flutter build hap后在DevEco Studio上启动项目报错的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
目前开发flutter鸿蒙项目,建议使用API12,即鸿蒙SDK版本是5.0.0。这样报错就会少些,因为现有的很多插件是基于api12版本进行的兼容处理。
在HarmonyOS Next中使用Flutter 3.22.1-ohos-1.0.4构建HAP后,在DevEco Studio启动报错,可能原因包括:Flutter for OpenHarmony版本与当前HarmonyOS Next的API或构建工具链不兼容;HAP包签名配置或依赖项缺失;DevEco Studio环境或项目配置文件(如build-profile.json5)存在冲突。需检查Flutter for OpenHarmony插件版本是否适配HarmonyOS Next,并确认构建配置正确。
这个错误表明你使用的 flutter_ohos 插件版本与当前 HarmonyOS Next(SDK 5.0.2)的 API 不兼容。具体来说,是插件中引用的 inputMethod 模块缺少 RequestKeyboardReason 这个成员。
根本原因:
RequestKeyboardReason 是 HarmonyOS SDK 中 @ohos.inputMethod 模块的一个枚举类型,用于指定请求输入法键盘的原因。在 SDK 的某些版本(可能是 5.0.2 或更早的某个版本)中,这个 API 可能发生了变更(例如被移除、重命名或修改了访问路径),导致基于旧版本 SDK 编译的 flutter_ohos 插件在 DevEco Studio 中使用新版本 SDK 进行 ArkTS 类型检查时失败。
解决方案:
你需要确保开发环境中所有组件的版本是匹配的。核心是让 flutter_ohos 插件与你的 HarmonyOS SDK 版本兼容。
-
检查并更新
flutter_ohos插件版本:- 首先,查看是否有更新的、明确支持 HarmonyOS SDK 5.0.2 的
flutter_ohos插件版本。可以查阅 Flutter for HarmonyOS 的官方发布说明或仓库。 - 在你的 Flutter 项目目录中,运行
flutter pub upgrade或flutter pub get来尝试获取最新的、兼容的插件版本。
- 首先,查看是否有更新的、明确支持 HarmonyOS SDK 5.0.2 的
-
核对 HarmonyOS SDK 版本:
- 确认你的 DevEco Studio 中安装的 HarmonyOS SDK 版本。你提到使用的是 5.0.2,请确保这是你项目实际编译所依赖的 SDK 版本(在
build-profile.json5或module.json5中检查compileSdkVersion和compatibleSdkVersion)。 - 有时,
flutter_ohos插件可能要求一个特定范围的 SDK 版本。如果插件版本较新,可能需要将你的项目 SDK 升级到插件所要求的版本(例如 5.0.3 或更高)。反之,如果插件版本较旧,你可能需要暂时降级项目的 SDK 版本以匹配插件。
- 确认你的 DevEco Studio 中安装的 HarmonyOS SDK 版本。你提到使用的是 5.0.2,请确保这是你项目实际编译所依赖的 SDK 版本(在
-
清理与重建:
- 在尝试更新版本后,执行完整的清理和重建操作:
- 删除项目中的
oh_modules目录和build目录。 - 在项目根目录运行
flutter clean。 - 重新运行
flutter pub get。 - 最后再执行
flutter build hap。
- 删除项目中的
- 在尝试更新版本后,执行完整的清理和重建操作:
当前最直接的推断:
你使用的 flutter 3.22.1-ohos-1.0.4 版本及其对应的 flutter_ohos 插件,很可能是基于比 HarmonyOS SDK 5.0.2 更早的 API 规范构建的。因此,当 DevEco Studio 使用 SDK 5.0.2 的 ArkTS 编译器对插件代码进行类型检查时,发现其中引用的 inputMethod.RequestKeyboardReason 在当前 SDK 中不存在。
总结:
版本不匹配是导致此问题的核心。你需要将 Flutter for HarmonyOS 工具链(包括 Flutter 引擎和 flutter_ohos 插件)与 HarmonyOS SDK 的版本对齐。优先尝试更新 flutter_ohos 插件到最新版本,如果问题依旧,则需要查阅官方文档,找到与 SDK 5.0.2 明确匹配的 Flutter for HarmonyOS 工具链版本组合。

