Flutter 无法加载资产

时间:2021-02-22 14:12:16

标签: flutter

我无法加载我的资产图片和图标。 可能是什么问题,我无法升级 Flutter 版本,因为如果它应该是答案,它会抛出很多错误。

BottomNavigationBarItem(
                icon: ImageIcon(
                  AssetImage("assets/icons/cash-back.png"),
                  color: Color(0xFF3A5A98),
                ),
                label: 'leagues',
                backgroundColor: AppColors.white,
              ),

enter image description here

错误:

════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/icons/cash-back.png

When the exception was thrown, this was the stack
#0      PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:227
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:673
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "assets/icons/cash-back.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#c10e3(), name: "assets/icons/cash-back.png", scale: 1.0)

3 个答案:

答案 0 :(得分:1)

要包含目录下的所有资产,请以/字符结尾指定目录名称,并将所有数据保存在特定目录中(所有图像在图像文件夹中)

assets:
    - assets/images/
    - assets/videos/

然后运行 ​​pub get

发布成功后,您可以参考您的资产,例如

Image.asset(
 'assets/images/trash.png',
  scale: 1.8,
),

答案 1 :(得分:1)

在您的图像中,查看您指定资产的位置。

flutter:
#some comments
assets:
  - assets/

注意你如何将“assets:”和“flutter:”放在同一个缩进处。在 pubspec.yaml 中,空格很重要。确保您的缩进如下所示:

flutter:
#some comments
  assets:
    - assets/

注释会使这个错误很容易被遗漏,所以一定要非常注意缩进。每个子项应比其父项缩进一个制表符或两个空格。

答案 2 :(得分:0)

将此包含在您的 pubspec.yaml 文件中,

flutter:
  assets:
     - assets/
     - assets/icons/

有关更多信息,请查看此链接:Adding assets and images