flutter - 无法加载资产
问题描述
这是我的 pubspec.yaml 文件
flutter:
# The following line ensures that the Material Icons font is
# included with your application so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/m1.jpg
- assets/m2.jpg
- assets/m3.jpg
- assets/m4.jpg
- assets/cat/m13.jpg
除了这张图片,上面的图片都在工作
- assets/cat/m13.jpg
我在 pubspec.yaml 文件中没有收到错误,并且运行“flutter packages get”给出的退出代码为 0。
在 HorizontalListView.dart 我有以下课程
class _HorizontalListViewState extends State<HorizontalListView> {
@override
Widget build(BuildContext context) {
return Container(
height: 80.0,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
Category(
image_location: " assets/cat/m13.jpg",
image_caption: "shirts",
),
],
),
);
}
}
我面临的错误是
The following assertion was thrown resolving an image codec:
I/flutter (17029): Unable to load asset: assets/cat/m13.jpg
I/flutter (17029):
I/flutter (17029): When the exception was thrown, this was the stack:
I/flutter (17029): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter (17029): <asynchronous suspension>
I/flutter (17029): #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:484:44)
I/flutter (17029): #2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:469:14)
I/flutter (17029): #3 ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
I/flutter (17029): #4 ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:160:22)
I/flutter (17029): #5 ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:325:84)
I/flutter (17029): (elided 13 frames from package dart:async)
I/flutter (17029):
I/flutter (17029): Image provider: AssetImage(bundle: null, name: " assets/cat/m13.jpg")
I/flutter (17029): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#5adcb(), name: " assets/cat/m13.jpg",
提前致谢
解决方案
正确检查文件夹名称,图像文件名和图像扩展名。我相信是否加载了除一张以外的所有其他图像。然后
不正确:
资产:
- 资产/m1.jpg
- 资产/m2.jpg
- 资产/m3.jpg
- 资产/m4.jpg
- 资产/猫/m13.jpg
正确的:
资产:
- 资产/m1.jpg
- 资产/m2.jpg
- 资产/m3.jpg
- 资产/m4.jpg
- 资产/validFolderName/validImageName.validExtension
例如: assets/cats/m13.jpg 或 assets/cat/m134.jpg 或 assets/cat/m13.png
推荐阅读
- javascript - 我怎样才能使这个 API 数据操作更干净?
- mysql - 结合多个 MySQL UNION 查询进行优化
- ruby - 在 Ruby 中使用 ERB 类时删除模板中的空格
- kubernetes - GCE Kubernetes:持久磁盘和持久卷声明
- python - 计算 Dataframe 中具有 1 个或多个 NaN 的行
- javascript - 为什么这种排序结果是断断续续的?
- javascript - 将 JS 转换为 jQuery 多实例的点击函数
- ios - iOS 开发环境设置问题
- r - 如何根据 radioButtons 的 inputId 值在 Shiny 服务器中执行功能?
- javascript - 如何安全地运行/包含/要求远程 javascript?