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

Flutter中Unable to load assets问题求助:配置Pubspec.yaml仍加载失败

Flutter图片加载失败(Assets not found)排查方案

检查Pubspec.yaml配置完整性

确保你的Pubspec.yaml中assets配置处于正确层级,且缩进格式正确(必须用2个空格,不能用Tab):

flutter:
  assets:
    - images/blank-profile-picture-973460_640.png

如果需要加载整个images文件夹的资源,可简化配置:

flutter:
  assets:
    - images/

清理缓存并重启服务

  1. 执行命令清除项目缓存:
flutter clean
  1. 重新获取依赖包:
flutter pub get
  1. 完全停止当前调试进程,重新运行App(热重载/热重启无法识别新增的资产资源变更)

验证路径与大小写一致性

  • 确认图片文件名、路径和代码中的引用完全一致,Flutter在iOS/macOS环境下区分大小写,比如Blank-Profile.png和blank-profile.png会被识别为不同文件
  • 确保images文件夹位于项目根目录(与lib、pubspec.yaml同级),而非lib文件夹内部

代码引用检查

你的代码中图片引用路径需与Pubspec.yaml配置完全匹配:

Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        gradient: LinearGradient(
          colors: gradientcolors,
          begin: startAlignment,
          end: endAlignment,
        ),
      ),
      child: Center(
        child: Image.asset('images/blank-profile-picture-973460_640.png'),
      ),
    );
  }

额外排查点

  • 确认图片文件未被误删或移动,实际存在于指定路径
  • 若使用Android Studio/IntelliJ,可右键点击Pubspec.yaml选择Flutter -> Pub Get,确保配置被IDE正确识别

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 02:47:08