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

Flutter项目启动失败:Gradle task assembleDebug执行报错

解决Flutter启动时ThemeData.errorColor未定义的编译错误

问题根源

这个错误是因为Flutter 3.16及以上版本移除了ThemeData.errorColor属性,替代方案是使用Theme.of(context).colorScheme.error。而你当前依赖的flutter_typeahead: 3.2.7版本没有适配这个Flutter的API变更,导致编译失败。你之前清理Gradle缓存的操作针对的是Gradle构建问题,和这个Dart代码层面的API不兼容无关,所以无法解决问题。

解决方案

方案1:升级flutter_typeahead到兼容版本(推荐)

  1. 打开项目根目录的pubspec.yaml文件,找到flutter_typeahead依赖项,修改为最新兼容版本(比如当前最新稳定版^4.8.0):
dependencies:
  flutter_typeahead: ^4.8.0
  1. 在终端执行命令更新依赖:
flutter pub get
  1. 重新运行项目:
flutter run

方案2:临时修改本地缓存的包文件(不推荐,仅临时应急)

  1. 定位到报错的文件:C:/Users/Asus/AppData/Local/Pub/Cache/hosted/pub.dev/flutter_typeahead-3.2.7/lib/src/flutter_typeahead.dart
  2. 打开文件找到第1248行,将:
style: TextStyle(color: Theme.of(context).errorColor),

替换为:

style: TextStyle(color: Theme.of(context).colorScheme.error),
  1. 保存文件后重新运行项目。

方案3:降级Flutter到兼容的旧版本

如果暂时不想升级依赖包,可以将Flutter降级到3.16以下的版本,比如3.13.9:

flutter downgrade 3.13.9

降级完成后重新运行项目即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 09:05:01