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

Flutter项目构建失败:dart:html相关依赖错误求助

Flutter构建报错:dart:html未找到及WebSocket相关错误

错误本质

你遇到的问题核心是**dart:html是仅Web平台可用的Dart库**,但当前在非Web环境(比如Windows桌面、移动端)构建项目时,你使用的GetX 4.6.5版本中,sockets_html.dart文件错误地被引入,导致编译时找不到dart:html,进而引发一系列WebSocket相关的类型错误。

解决方案

1. 升级GetX到最新稳定版

GetX旧版本存在跨平台WebSocket适配缺陷,新版本已修复该问题。执行以下命令升级:

flutter pub upgrade get

升级完成后清理构建缓存并重新获取依赖:

flutter clean
flutter pub get

之后重新构建项目即可。

2. 手动指定多平台WebSocket实现

如果暂时无法升级GetX,可在项目的lib/main.dart最顶部添加以下代码,强制使用支持多平台的WebSocket实现:

import 'package:get/get_connect/sockets/src/sockets_io.dart' as sockets;
export 'package:get/get_connect/sockets/src/sockets_io.dart';

这会覆盖默认的Web端sockets_html.dart,改用适配多平台的sockets_io.dart。

3. 确认构建目标平台

如果你的目标是构建Web项目,确保使用Web平台的运行命令:

flutter run -d chrome

避免在非Web平台下运行Web专属代码。

原始错误日志

Invalid depfile: C:\Users\ajmal\OneDrive\Desktop\Flutter projects\Firebase project -week16\chatapp\.dart_tool\flutter_build\2bc53116d09bd81691d0c1eb73277574\kernel_snapshot.d

: Error: Not found: 'dart:html'
package:get/…/src/sockets_html.dart:5
import 'dart:html';      
       ^
Error: Type 'WebSocket' not found.
package:get/…/src/sockets_html.dart:12
  WebSocket? socket;
  ^^^^^^^^^
: Error: 'WebSocket' isn't a type.
package:get/…/src/sockets_html.dart:12
  WebSocket? socket;
  ^^^^^^^^^
: Error: The method 'WebSocket' isn't defined for the class 'BaseWebSocket'.
package:get/…/src/sockets_html.dart:38
- 'BaseWebSocket' is from 'package:get/get_connect/sockets/src/sockets_html.dart' ('../../../../Documents/flutter_windows_2.10.0-stable/flutter/.pub-cache/hosted/pub.dartlang.org/get-4.6.5/lib/get_connect/sockets/src/sockets_html.dart').
package:get/…/src/sockets_html.dart:1
Try correcting the name to the name of an existing method, or defining a method named 'WebSocket'.
      socket = WebSocket(url);
               ^^^^^^^^^
: Error: The getter 'WebSocket' isn't defined for the class 'BaseWebSocket'.
package:get/…/src/sockets_html.dart:105
- 'BaseWebSocket' is from 'package:get/get_connect/sockets/src/sockets_html.dart' ('../../../../Documents/flutter_windows_2.10.0-stable/flutter/.pub-cache/hosted/pub.dartlang.org/get-4.6.5/lib/get_connect/sockets/src/sockets_html.dart').
package:get/…/src/sockets_html.dart:1
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WebSocket'.
    if (socket != null && socket!.readyState == WebSocket.OPEN) {

                                                ^^^^^^^^^
Unhandled exception:
FileSystemException(uri=org-dartlang-untranslatable-uri:dart%3Ahtml; 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:652:37)
#2      writeDepfile (package:vm/kernel_front_end.dart:792:21)

<asynchronous suspension>
#3      FrontendCompiler.compile (file:///C:/b/s/w/ir/cache/builder/sdk/pkg/frontend_server/lib/frontend_server.dart:615:9)
<asynchronous suspension>
#4      starter (file:///C:/b/s/w/ir/cache/builder/sdk/pkg/frontend_server/lib/frontend_server.dart:1433:12)
<asynchronous suspension>

5      main (file:///C:/b/s/w/ir/cache/builder/sdk/pkg/frontend_server/bin/frontend_server_starter.dart:10:14)
<asynchronous suspension>
4

FAILURE: Build failed with an exception.

* Where:
Script 'C:\Users\ajmal\OneDrive\Documents\flutter_windows_2.10.0-stable\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1156

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\Users\ajmal\OneDrive\Documents\flutter_windows_2.10.0-stable\flutter\bin\flutter.bat'' finished with non-zero exit value 1

 Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 05:24:10