【问题标题】:Unable to load assets image [duplicate]无法加载资产图像[重复]
【发布时间】:2019-08-14 02:17:39
【问题描述】:

我想制作一个简单的颤振应用程序并想从我的资产中添加一张图片,我已经尝试过,但图片仍然无法加载。 这就是我加载图像的方式

Image.asset('assets/images/food.jpg')

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/images/food.jpg

调试:

I/flutter (18975): The following assertion was thrown resolving an image codec:
I/flutter (18975): Unable to load asset: assets/images/food.jpg
I/flutter (18975): Unable to load asset: assets/images/food.jpg
I/flutter (18975):
I/flutter (18975): When the exception was thrown, this was the stack:
I/flutter (18975): #0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
I/flutter (18975): <asynchronous suspension>
I/flutter (18975): #1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:433:44)
I/flutter (18975): <asynchronous suspension>
I/flutter (18975): #2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:418:14)
I/flutter (18975): #3      ImageProvider.resolve.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:285:105)
I/flutter (18975): #4      ImageCache.putIfAbsent (package:flutter/src/painting/image_cache.dart:157:22)
I/flutter (18975): #5      ImageProvider.resolve.<anonymous closure> (package:flutter/src/painting/image_provider.dart:285:82)
I/flutter (18975): (elided 13 frames from package dart:async)
I/flutter (18975):
I/flutter (18975): Image provider: AssetImage(bundle: null, name: "assets/images/food.jpg")
I/flutter (18975): Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#df91c(), name: "assets/images/food.jpg",
I/flutter (18975): scale: 1.0)

这是我的图片文件夹的样子:

【问题讨论】:

    标签: image flutter


    【解决方案1】:

    您的格式不符合 pubspec.yaml 中的要求。从代码中可以看出,您必须添加一个选项卡。

    像这样:

    flutter:
       assets:
       - graphics/background.png
    

    更多详情请看这里。 https://flutter.dev/docs/development/ui/assets-and-images

    【讨论】:

      猜你喜欢
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      • 2018-11-23
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      相关资源
      最近更新 更多