You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter构建APK报错:‘dart:js_interop’当前平台不可用

解决Flutter构建APK时dart:js_interop平台不兼容错误

问题场景

执行flutter build apk --debug构建Flutter项目时触发以下错误:

lib/screens/ListPage.dart:1:8: Error: Dart library 'dart:js_interop' is not available on this platform.
import 'dart:js_interop';
       ^
Context: The unavailable library 'dart:js_interop' is imported through these packages:

    package:easy_cost_splitter => dart:js_interop

Detailed import paths for (some of) the these imports:

    package:easy_cost_splitter/main.dart => package:easy_cost_splitter/screens/ListPage.dart => dart:js_interop

Unhandled exception:
FileSystemException(uri=org-dartlang-untranslatable-uri:dart%3Ajs_interop; message=StandardFileSystem only supports file:* and data:* URIs)
#0      StandardFileSystem.entityForUri (package:front_end/src/api_prototype/standard_file_system.dart:34:7)
#1      asFileUri (package:vm/kernel_front_end.dart:721:37)
#2      writeDepfile (package:vm/kernel_front_end.dart:861:21)
<asynchronous suspension>
#3      FrontendCompiler.compile (package:frontend_server/frontend_server.dart:669:9)
<asynchronous suspension>
#4      starter (package:frontend_server/starter.dart:102:12)
<asynchronous suspension>
#5      main (file:///b/s/w/ir/x/w/sdk/pkg/frontend_server/bin/frontend_server_starter.dart:13:14)
<asynchronous suspension>

Target kernel_snapshot failed: Exception


FAILURE: Build failed with an exception.

* Where:
Script '/home/myname/flutter/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy' line: 1297

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command '/home/myname/flutter/bin/flutter'' finished with non-zero exit value 1

但报错指向的ListPage.dart代码中并无dart:js_interop相关引用:

class ListPage extends StatefulWidget {
  const ListPage({super.key});

  @override
  State<ListPage> createState() => _ListPageState();
}

已尝试以下操作但未解决:

  • 全局搜索项目目录未找到js_interop相关内容
  • 执行flutter clean后重新flutter pub get
  • 移除新安装的CMake等扩展
  • 重启VS Code及电脑

flutter doctor结果无异常:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.13.9, on Linux Mint 21.1 5.15.0-88-generic, locale de_DE.UTF-8)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2022.2)
[✓] VS Code (version 1.83.1)
[✓] Connected device (2 available)
[✓] Network resources

• No issues found!

问题原因

dart:js_interop是Dart专为Web平台提供的库,Android等移动端平台不支持该库。错误提示显示依赖链中存在间接引入该库的情况——可能是某个第三方依赖包未做跨平台兼容处理,在非Web环境下仍引入了Web专用代码。

解决方案

  1. 排查第三方依赖包

    • 执行flutter pub deps生成依赖树,检查所有依赖包,重点排查最近新增的包。
    • 临时注释pubspec.yaml中的依赖项,逐个重新构建测试,定位导致问题的包。找到后:
      • 尝试升级该包到最新版本,看是否已修复跨平台兼容问题;
      • 若新版本仍存在问题,可替换为功能类似的兼容包,或向包作者提交issue反馈。
  2. 检查依赖包的平台适配代码

    • 进入.pub-cache/hosted/pub.dartlang.org/目录,找到对应依赖包的源码,搜索是否存在未加平台条件判断的dart:js_interop引入。
    • 若发现问题代码,可临时修改包源码(添加平台条件编译),或等待官方修复。
  3. 添加平台条件编译/导入

    • 如果是项目自身代码间接引入了Web专用库,使用条件导入分离平台代码:
      // 示例:根据平台导入不同工具类
      import 'web_utils.dart' if (dart.library.io) 'mobile_utils.dart';
      
    • 对于必须使用dart:js_interop的场景,添加平台注解限制仅在Web环境生效:
      @dart:js_interop
      import 'dart:js_interop' show JSObject;
      
  4. 深度清理构建缓存

    • 除了flutter clean,手动删除项目根目录下的.dart_tool、build目录,然后执行flutter pub get重新拉取依赖,再尝试构建。
  5. 确认构建目标平台

    • 检查IDE或命令行的构建目标是否为Android,避免误选Web平台导致的依赖加载错误。

内容的提问来源于stack exchange,提问作者Rasputin221

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 22:05:54