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

Flutter应用运行失败求助:pubspec.yaml找不到字体资产条目

Flutter编译失败:找不到字体资源文件的解决办法

错误信息

[Error: unable to locate asset entry in pubspec.yaml: "fonts/googlesansebold.ttf".]

FAILURE: Build failed with an exception.

* Where:
Script 'C:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1159

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\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.

* Get more help at https://help.gradle.org

BUILD FAILED in 2m 17s
Exception: Gradle task assembleDebug failed with exit code 1

解决步骤

  • 检查pubspec.yaml资源配置
    确保pubspec.yaml中正确声明字体资源,注意YAML的缩进规则(必须用2个空格)和文件名拼写:

    flutter:
      uses-material-design: true
      # 作为普通资源引用
      assets:
        - fonts/googlesansebold.ttf
      # 作为自定义字体引用(如需使用字体样式)
      fonts:
        - family: GoogleSans
          fonts:
            - asset: fonts/googlesansebold.ttf
              weight: 700
    
  • 验证文件实际存在
    确认项目根目录下存在fonts文件夹,且文件夹内确实有googlesansebold.ttf文件。注意文件名大小写要和pubspec.yaml中完全一致(Flutter资源查找区分大小写,即使Windows系统不区分)。

  • 清理缓存并重新构建
    依次执行以下命令:

    flutter clean
    flutter pub get
    flutter run
    
  • 排除Gradle干扰
    本次错误核心是资源文件缺失,Gradle报错为连锁反应。无需重新下载Gradle,确保Flutter SDK路径配置正确即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 12:43:29