HarmonyOS鸿蒙Next中Flutter适配报错
HarmonyOS鸿蒙Next中Flutter适配报错
将已有项目适配鸿蒙
原项目Flutter 3.35.5 Java 25
更改
安装使用 3.27.5-ohos-1.0.0 java 17
C:\Users\10169>fvm flutter doctor -v
┌─────────────────────────────────────────────────────────────────┐
│ ⚠ Flutter SDK: (3.27.5-ohos-1.0.0) is not valid Flutter version │
└─────────────────────────────────────────────────────────────────┘
✔ Do you want to continue? · yes
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
[!] Flutter (Channel [user-branch], 3.27.5-ohos-1.0.0, on Microsoft Windows [版本 10.0.26100.6899], locale zh-CN)
! Flutter version 3.27.5-ohos-1.0.0 on channel [user-branch] at C:\Users\10169\fvm\versions\3.27.5-ohos-1.0.0
Currently on an unknown channel. Run `flutter channel` to switch to an official channel.
If that doesn't fix the issue, reinstall Flutter by following instructions at https://flutter.dev/setup.
• Upstream repository https://gitcode.com/openharmony-tpc/flutter_flutter.git
• FLUTTER_GIT_URL = https://gitcode.com/openharmony-tpc/flutter_flutter.git
• Framework revision 4c5ae9f27d (13 days ago), 2025-10-11 11:28:53 +0800
• Engine revision e672b006cb
• Dart version 3.6.2
• DevTools version 2.40.0
• Pub download mirror https://pub.flutter-io.cn
• Flutter download mirror https://storage.flutter-io.cn
• If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly
to perform update checks and upgrades.
[√] HarmonyOS toolchain - develop for HarmonyOS devices
• OpenHarmony Sdk at C:\Program Files\Huawei\DevEco Studio\sdk, available api versions has [20:default]
• Ohpm version 5.3.2
• Node version v18.12.1
• Hvigorw binary at C:\Users\10169\command-line-tools\bin\hvigorw.bat
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 36.1.0)
• Android SDK at C:\Users\10169\AppData\Local\Android\sdk
• Platform android-36, build-tools 36.1.0
• Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
• Java version OpenJDK Runtime Environment (build 21.0.7+-13880790-b1038.58)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.14.16)
• Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
• Visual Studio Community 2022 version 17.14.36518.9
• Windows 10 SDK version 10.0.26100.0
[√] Android Studio (version 2025.1.3)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 21.0.7+-13880790-b1038.58)
[√] VS Code (version 1.105.1)
• VS Code at C:\Users\10169\AppData\Local\Programs\Microsoft VS Code
• Flutter extension can be installed from:
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[√] Connected device (4 available)
• 127.0.0.1:5555 (mobile) • 127.0.0.1:5555 • ohos-x64 • Ohos OpenHarmony-6.0.0.47 (API 20)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [版本 10.0.26100.6899]
• Chrome (web) • chrome • web-javascript • Google Chrome 141.0.7390.108
• Edge (web) • edge • web-javascript • Microsoft Edge 141.0.3537.92
[√] Network resources
• All expected network resources are available.
! Doctor found issues in 1 category.
安装ohos
C:\Users\10169\Desktop\ProjectFile\AmsyFile>fvm flutter create --platforms ohos easy_learn
┌─────────────────────────────────────────────────────────────────┐
│ ⚠ Flutter SDK: (3.27.5-ohos-1.0.0) is not valid Flutter version │
└─────────────────────────────────────────────────────────────────┘
✔ Do you want to continue? · yes
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Recreating project easy_learn...
easy_learn\ohos\.gitignore (created)
easy_learn\ohos\AppScope\app.json5 (created)
easy_learn\ohos\AppScope\resources\base\element\string.json (created)
easy_learn\ohos\AppScope\resources\base\media\app_icon.png (created)
easy_learn\ohos\build-profile.json5 (created)
easy_learn\ohos\entry\.gitignore (created)
easy_learn\ohos\entry\build-profile.json5 (created)
easy_learn\ohos\entry\hvigorfile.ts (created)
easy_learn\ohos\entry\oh-package.json5 (created)
easy_learn\ohos\entry\src\main\ets\entryability\EntryAbility.ets (created)
easy_learn\ohos\entry\src\main\ets\pages\Index.ets (created)
easy_learn\ohos\entry\src\main\module.json5 (created)
easy_learn\ohos\entry\src\main\resources\base\element\color.json (created)
easy_learn\ohos\entry\src\main\resources\base\element\string.json (created)
easy_learn\ohos\entry\src\main\resources\base\media\icon.png (created)
easy_learn\ohos\entry\src\main\resources\base\profile\buildinfo.json5 (created)
easy_learn\ohos\entry\src\main\resources\base\profile\framesconfig.json (created)
easy_learn\ohos\entry\src\main\resources\base\profile\main_pages.json (created)
easy_learn\ohos\entry\src\main\resources\en_US\element\string.json (created)
easy_learn\ohos\entry\src\main\resources\zh_CN\element\string.json (created)
easy_learn\ohos\entry\src\ohosTest\ets\test\Ability.test.ets (created)
easy_learn\ohos\entry\src\ohosTest\ets\test\List.test.ets (created)
easy_learn\ohos\entry\src\ohosTest\ets\testability\pages\Index.ets (created)
easy_learn\ohos\entry\src\ohosTest\ets\testability\TestAbility.ets (created)
easy_learn\ohos\entry\src\ohosTest\ets\testrunner\OpenHarmonyTestRunner.ts (created)
easy_learn\ohos\entry\src\ohosTest\module.json5 (created)
easy_learn\ohos\entry\src\ohosTest\resources\base\element\color.json (created)
easy_learn\ohos\entry\src\ohosTest\resources\base\element\string.json (created)
easy_learn\ohos\entry\src\ohosTest\resources\base\media\icon.png (created)
easy_learn\ohos\entry\src\ohosTest\resources\base\profile\test_pages.json (created)
easy_learn\ohos\hvigor\hvigor-config.json5 (created)
easy_learn\ohos\hvigorconfig.ts (created)
easy_learn\ohos\hvigorfile.ts (created)
easy_learn\ohos\oh-package.json5 (created)
Resolving dependencies in `easy_learn`...
Downloading packages...
Got dependencies in `easy_learn`.
Wrote 34 files.
All done!
You can find general documentation for Flutter at: https://docs.flutter.dev/
Detailed API documentation is available at: https://api.flutter.dev/
If you prefer video documentation, consider: https://www.youtube.com/c/flutterdev
In order to run your application, type:
$ cd easy_learn
$ flutter run
Your application code is in easy_learn\lib\main.dart.
存在下述报错:
在DevEco Studio,选择模拟器运行时报错如下
"C:\Program Files\Huawei\DevEco Studio\tools\node\node.exe" "C:\Program Files\Huawei\DevEco Studio\tools\hvigor\bin\hvigorw.js" --mode module -p module=entry@default -p product=default -p requiredDeviceType=phone assembleHap --analyze=normal --parallel --incremental --daemon
> hvigor start to execute ohpm install
ohpm INFO: remove useless folder succeed: "C:\Users\10169\Desktop\ProjectFile\AmsyFile\easy_learn\ohos\oh_modules\.ohpm\@ohos+flutter_ohos@zk+twenx0aihrvpsj+wn3nniopx7gpinrehsd3+krim="
ohpm INFO: remove useless folder succeed: "C:\Users\10169\Desktop\ProjectFile\AmsyFile\easy_learn\ohos\oh_modules\.ohpm\flutter_native_arm64_v8a@h0ralplazt2hdjp9zswxt0nknfbwadpl13dxmimk1po="
ohpm INFO: remove useless folder succeed: "C:\Users\10169\Desktop\ProjectFile\AmsyFile\easy_learn\ohos\oh_modules\.ohpm\flutter_native_x86_64@7iwli46f1wf+ytxkv6iuhzoutwu99clpmkomo+ikatu="
ohpm INFO: remove useless folder succeed: "C:\Users\10169\Desktop\ProjectFile\AmsyFile\easy_learn\ohos\oh_modules\.tmp"
install completed in 0s 721ms
> hvigor WARN: The project has not explicitly set the 'targetSdkVersion' version at build-profile.json5. It is recommended to configure it. Reference: https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/ide-hvigor-build-profile-app#section45865492619
> hvigor Finished :entry:default@PreBuild... after 68 ms
> hvigor Finished :fluwx:default@PreBuild... after 65 ms
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
../../../../AppData/Local/Pub/Cache/hosted/pub.flutter-io.cn/modal_bottom_sheet-3.0.0/lib/src/bottom_sheet_route.dart:36:13: Error: The type 'TargetPlatform' is not exhaustively matched by the switch cases since it doesn't match 'TargetPlatform.ohos'.
- 'TargetPlatform' is from 'package:flutter/src/foundation/platform.dart' ('../../../../fvm/versions/3.27.5-ohos-1.0.0/packages/flutter/lib/src/foundation/platform.dart').
Try adding a default case or cases that match 'TargetPlatform.ohos'.
switch (platform) {
^
../../../../AppData/Local/Pub/Cache/hosted/pub.flutter-io.cn/modal_bottom_sheet-3.0.0/lib/src/bottom_sheet.dart:492:11: Error: The type 'TargetPlatform' is not exhaustively matched by the switch cases since it doesn't match 'TargetPlatform.ohos'.
- 'TargetPlatform' is from 'package:flutter/src/foundation/platform.dart' ('../../../../fvm/versions/3.27.5-ohos-1.0.0/packages/flutter/lib/src/foundation/platform.dart').
Try adding a default case or cases that match 'TargetPlatform.ohos'.
switch (platform) {
^
../../../../AppData/Local/Pub/Cache/hosted/pub.flutter-io.cn/chewie-1.11.3/lib/src/helpers/adaptive_controls.dart:11:31: Error: The type 'TargetPlatform' is not exhaustively matched by the switch cases since it doesn't match 'TargetPlatform.ohos'.
- 'TargetPlatform' is from 'package:flutter/src/foundation/platform.dart' ('../../../../fvm/versions/3.27.5-ohos-1.0.0/packages/flutter/lib/src/foundation/platform.dart').
Try adding a default case or cases that match 'TargetPlatform.ohos'.
switch (Theme.of(context).platform) {
^
Target kernel_snapshot_program failed: Exception
> hvigor ERROR: Failed :entry:default@FlutterTask...
> hvigor ERROR: Command failed: C:\Users\10169\fvm\versions\3.27.5-ohos-1.0.0\bin\flutter.bat --quiet assemble --no-version-check --depfile build/ohos/intermediates/flutter/defaultDebug/flutter_build.d --output build/ohos/intermediates/flutter/defaultDebug -dTargetFile=lib/main.dart -dTargetPlatform=ohos -dBuildMode=debug -dOhosArchs=ohos-arm64 ohos-x64 debug_ohos_application
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
../../../../AppData/Local/Pub/Cache/hosted/pub.flutter-io.cn/modal_bottom_sheet-3.0.0/lib/src/bottom_sheet_route.dart:36:13: Error: The type 'TargetPlatform' is not exhaustively matched by the switch cases since it doesn't match 'TargetPlatform.ohos'.
- 'TargetPlatform' is from 'package:flutter/src/foundation/platform.dart' ('../../../../fvm/versions/3.27.5-ohos-1.0.0/packages/flutter/lib/src/foundation/platform.dart').
Try adding a default case or cases that match 'TargetPlatform.ohos'.
switch (platform) {
^
../../../../AppData/Local/Pub/Cache/hosted/pub.flutter-io.cn/modal_bottom_sheet-3.0.0/lib/src/bottom_sheet.dart:492:11: Error: The type 'TargetPlatform' is not exhaustively matched by the switch cases since it doesn't match 'TargetPlatform.ohos'.
- 'TargetPlatform' is from 'package:flutter/src/foundation/platform.dart' ('../../../../fvm/versions/3.27.5-ohos-1.0.0/packages/flutter/lib/src/foundation/platform.dart').
Try adding a default case or cases that match 'TargetPlatform.ohos'.
switch (platform) {
^
../../../../AppData/Local/Pub/Cache/hosted/pub.flutter-io.cn/chewie-1.11.3/lib/src/helpers/adaptive_controls.dart:11:31: Error: The type 'TargetPlatform' is not exhaustively matched by the switch cases since it doesn't match 'TargetPlatform.ohos'.
- 'TargetPlatform' is from 'package:flutter/src/foundation/platform.dart' ('../../../../fvm/versions/3.27.5-ohos-1.0.0/packages/flutter/lib/src/foundation/platform.dart').
Try adding a default case or cases that match 'TargetPlatform.ohos'.
switch (Theme.of(context).platform) {
^
Target kernel_snapshot_program failed: Exception
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --debug option to get more log output.
> hvigor ERROR: BUILD FAILED in 8 s 207 ms
更多关于HarmonyOS鸿蒙Next中Flutter适配报错的实战教程也可以访问 https://www.itying.com/category-92-b0.html
尊敬的开发者您好,从日志发现报错来自于:modal_bottom_sheet-3.0.0、chewie-1.11.3,这两个三方库暂未适配导致的;
【解决方案】
1、chewie库已适配,请使用fluttertpc_chewie替代;
2、modal_bottom_sheet暂未适配,您可以在OpenHarmony-SIG组织和flutter_packages中寻找使用其他已适配的三方库替代,或自行适配,参考ohos平台适配flutter三方库指导。
更多关于HarmonyOS鸿蒙Next中Flutter适配报错的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在HarmonyOS Next中,Flutter适配报错通常是由于框架与系统API兼容性问题导致。鸿蒙Next不再支持Android底层依赖,Flutter引擎需调用鸿蒙原生接口。主要涉及UI渲染、平台通道及系统服务调用的适配。需检查Flutter for HarmonyOS分支版本,确认是否使用专为鸿蒙编译的Flutter引擎。常见错误包括Skia图形库渲染异常、Dart与ArkTS交互失败、以及系统权限声明缺失。需同步更新鸿蒙SDK与DevEco Studio插件,并在模块级build.gradle中配置鸿蒙依赖项。
你遇到的错误是由于第三方 Flutter 插件(modal_bottom_sheet 和 chewie)未适配 HarmonyOS 的 TargetPlatform.ohos 枚举值导致的。这些插件在 switch 语句中未处理 ohos 平台,因此编译失败。
解决方案:
-
检查并更新插件:确认这些插件是否有支持 HarmonyOS 的更新版本。访问 pub.dev 查看插件页面,或检查其 GitHub 仓库是否有相关提交。
-
临时修改插件源码:在
pubspec.yaml中使用dependency_overrides重写有问题的插件,为它们的 switch 语句添加case TargetPlatform.ohos:分支,或使用default分支处理未覆盖的情况。例如,在bottom_sheet_route.dart的 switch 块中添加:case TargetPlatform.ohos: // 复用 android 或其他平台的逻辑,或自定义 break; -
移除或替换插件:如果插件暂无适配计划,考虑寻找替代插件或暂时移除相关功能。
-
清理构建缓存:修改后运行
flutter clean并重新构建,确保更改生效。
此问题源于社区插件的平台兼容性,需等待官方或社区更新适配。

