【问题标题】:'Unable to load asset error' is caught by 'Image resource service'“图像资源服务”捕获“无法加载资产错误”
【发布时间】:2021-08-17 19:43:32
【问题描述】:

错误:

======= 图像资源服务捕获的异常 ================================== =============== 抛出以下断言来解析图像编解码器: 无法加载资产:images/google-logo.png

当异常被抛出时,这是堆栈: #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:227:7) #1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:667:14) 图片提供者:AssetImage(bundle: null, name: "images/google-logo.png") 图片键:AssetBundleImageKey(bundle: PlatformAssetBundle#36c1f(), name: "images/google-logo.png", scale: 1.0)

我的“pubspec.yaml”文件:

'''flutter:
     uses-material-design: true
     assets:
        - images/facebook-logo.png
        - images/google-logo.png '''

我正在使用以下代码将图像加载到页面中: IN '登录 page.dart' 文件:

'''CustomRaisedButton(
            child: Image.asset('images/google-logo.png'),
            color: Colors.white,
            onPressed: () {},
          ),'''

【问题讨论】:

  • 您是否在pubspec.yaml 中添加了资产文件夹?你可以关注这个话题stackoverflow.com/questions/50903106/…
  • @ChiragBargoojar 我确实在“pubspec.yaml”文件中添加了资产文件夹。
  • 能否分享您的pubspec.yaml 以及您创建资产文件夹的位置?

标签: flutter android-studio dart pubspec


【解决方案1】:

首先在 pubspec.yaml 文件的 assets 属性中设置您的图像路径,取消注释并为您的图像文件夹写入路径。,

flutter:

[2 whitespaces or 1 tab]assets:
[4 whitespaces or 2 tabs]- assets/images/ #path of your image folder

pub get 一次,就可以使用给定路径文件夹中的图片了。

如果您的问题尚未解决。我认为您的资产没有加载,请在终端中使用以下命令,

flutter clean flutter pub get

重新启动或重新安装应用程序。

【讨论】:

  • 谢谢。这是缩进问题。解决了
【解决方案2】:

指定图片路径如下

Image.asset("assets/images/car_android.png")

Image(image: AssetImage("assets/images/car_android.png"))

问题是 Flutter 无法识别您的图像的确切存储位置。

如果问题仍然存在,请尝试执行 HOT RESTART 这将解决您的问题。

如果您更新 pubspec.yaml 文件,如果您的 IDE 没有自动更新,则运行 flutter pub get 来更新库。

【讨论】:

    猜你喜欢
    • 2019-03-09
    • 2021-02-28
    • 2014-05-22
    • 2019-12-24
    • 2021-06-21
    • 2021-01-17
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    相关资源
    最近更新 更多