Flutter构建Wasm失败:firebase_core_web依赖dart:html平台不兼容报错
Flutter Wasm编译报错:dart:html库不可用的解决办法
问题根源
Flutter Web的实验性Wasm编译目标不支持dart:html这类仅适配JS编译环境的库,而你使用的firebase_core_web:2.5.0直接依赖了dart:html,触发了编译错误。
可行解决方案
升级Firebase依赖到兼容版本
新版本的Firebase Web包已经适配了Wasm编译目标,在pubspec.yaml中更新相关依赖:firebase_core: ^2.15.0 firebase_core_web: ^2.10.0执行
flutter pub get完成依赖更新后重新编译。使用条件导入适配Wasm环境(临时方案)
若暂时无法升级依赖,可通过条件导入在Wasm环境下跳过依赖dart:html的模块,示例代码:import 'firebase_core_web_js.dart' if (dart.library.wasm) 'firebase_core_web_wasm.dart';注意:这种方式需要自行实现Wasm环境下的Firebase初始化逻辑,仅作为临时 workaround。
确保Flutter SDK版本达标
确认使用的Flutter SDK版本在3.10及以上(支持Wasm编译的最低版本),执行flutter --version检查,版本过低则执行:flutter upgrade
内容的提问来源于stack exchange,提问作者entangled
相关产品推荐
相关产品推荐

