Flutter:无法加载资产图像提供者:AssetImage(bundle:null,名称:“assets/images/rose.jpg”)

Posted

技术标签:

【中文标题】Flutter:无法加载资产图像提供者:AssetImage(bundle:null,名称:“assets/images/rose.jpg”)【英文标题】:Flutter: Unable to load asset Image provider: AssetImage(bundle: null, name: "assets/images/rose.jpg") 【发布时间】:2021-05-11 02:26:44 【问题描述】:

我无法将图像作为背景上传到堆栈,我已将图像添加到 assets 文件夹中并将其添加到 pubspec.yaml 并显示该错误:

Exception caught by image resource service.
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/images/rose.jpg
When the exception was thrown, this was the stack: 
#0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:225:7)
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:668:31)
#2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:651:14)
#3      ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:504:13)
...
Image provider: AssetImage(bundle: null, name: "assets/images/rose.jpg")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#dea48(), name: "assets/images/rose.jpg", scale: 1.0)

代码:

  return new Scaffold(
  backgroundColor: Colors.greenAccent,

  body: new Stack(
    children: [
      new Image(
        image: new AssetImage("assets/images/rose.jpg"),
        fit: BoxFit.cover,
      ),
    ],
  ),

);

My Asset folder

【问题讨论】:

【参考方案1】:

运行 flutter clean 并再次运行。

【讨论】:

【参考方案2】:

您是否在 pubspec.yaml 文件中添加了资产?

assets:
    - assets/images/rose.jpg

【讨论】:

【参考方案3】:

请检查您的 pubspec.yaml 文件是否正确缩进,如下所示。不正确的间距/缩进可能会导致找不到图像。

flutter: 
  assets:
    - assets/images/rose.jpg

【讨论】:

【参考方案4】:

在您的终端上:

flutter pub get

刷新 pubspec.yaml

【讨论】:

以上是关于Flutter:无法加载资产图像提供者:AssetImage(bundle:null,名称:“assets/images/rose.jpg”)的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Flutter 中获取 Asset 对象的路径?

Flutter 无法在物理设备上加载图像资产(但在模拟器上加载它就好了)

如何在 Flutter web 上显示资产图像?

在颤振中使用动画 gif

Webpack 不加载“资产/资源”类型

在 Flutter 的 GridView 中显示资产图像?