首页 > 解决方案 > 无法加载资产

问题描述

这是我的 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。

在 Horizo​​ntalListView.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",



提前致谢

标签: flutterdartassets

解决方案


正确检查文件夹名称,图像文件名和图像扩展名。我相信是否加载了除一张以外的所有其他图像。然后

不正确:

资产:

  • 资产/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


推荐阅读