添加依赖后Flutter Web构建失败,提示dart:ffi未找到
问题描述
添加syncfusion_flutter_datagrid、printing和pdf包后,执行Flutter Web构建时提示找不到dart:ffi。已尝试flutter clean && flutter pub get命令,也在pubspec.yml中添加了ffi依赖,问题仍未解决;即使移除syncfusion_flutter_datagrid包,错误依然存在。
报错信息
Compiling lib/main.dart for the Web... 16.0s Target dart2js failed: Exception: Warning: The 'dart2js' entrypoint script is deprecated, please use 'dart compile js' instead. /root/.pub-cache/hosted/pub.dartlang.org/archive-3.3.3/lib/src/util/aes_decrypt.dart:1:8: Error: Not found: 'dart:ffi' import 'dart:ffi'; ^ /root/.pub-cache/hosted/pub.dartlang.org/syncfusion_flutter_datagrid-20.3.56/lib/src/datagrid_widget/widgets/cell_widget.dart:695:19: Warning: Operand of null-aware operation '!' has type 'OverlayState' which excludes null. - 'OverlayState' is from 'package:flutter/src/widgets/overlay.dart' ('/usr/local/flutter/packages/flutter/lib/src/widgets/overlay.dart'). Overlay.of(context)!.context.findRenderObject()! as RenderBox; ^ Error: Compilation failed. Exception: Failed to compile application for the Web. The command '/bin/sh -c flutter build web' returned a non-zero code: 1
Flutter环境检查结果
mark@mark-desktop:~/dev/example/cmd/client$ flutter doctor Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.3.7, on Ubuntu 22.04.1 LTS 5.15.0-52-generic, locale en_AU.UTF-8) [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0) [✓] Chrome - develop for the web [✓] Linux toolchain - develop for Linux desktop [✓] Android Studio (version 2021.3) [✓] VS Code [✓] Connected device (2 available) [✓] HTTP Host Availability • No issues found!
解决方案
- 问题根源:报错指向
archive-3.3.3包引入了dart:ffi,但dart:ffi是仅支持移动端/桌面端的API,Web环境无法兼容。该包是由你引入的printing或pdf等依赖间接拉取的。 - 具体解决步骤:
- 执行
flutter pub deps命令,查看依赖树,确认archive包的引入路径(验证是哪个依赖触发了不兼容版本)。 - 在
pubspec.yml中添加dependency_overrides,强制指定兼容Web的archive版本(推荐3.1.2,该版本未引入dart:ffi):dependency_overrides: archive: 3.1.2 - 重新执行依赖安装:
flutter clean && flutter pub get - 再次尝试构建Web:
flutter build web
- 执行
- 关于Syncfusion的警告:那个空安全操作符的警告不影响构建,可暂时忽略,或升级
syncfusion_flutter_datagrid到最新稳定版来修复。
内容的提问来源于stack exchange,提问作者markhorrocks
相关产品推荐
相关产品推荐

