Flutter:无法加载图片素材资源

时间:2020-10-01 15:29:06

标签: image flutter assets

这是我第一次使用flutter,紧随文档中介绍的所有内容并将图像添加到小部件。但是我得到了这个错误,我不明白哪里做错了

我尝试过这种方式

@override
  Widget build(BuildContext context) {
    return Container(
      child: Image.asset("drawables/ic_launcher_transparent.png", width : 100, height: 100),
    );
  }

  • 文件结构
    |-lib
    | ---可绘制
    | ----- ic_launcher_transparent.png

  • pubspec.yaml
    use-material-design:true
    资产:
    -绘画

我收到此错误

════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: drawables/ic_launcher_transparent.png

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: "drawables/ic_launcher_transparent.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#59d28(), name: "drawables/ic_launcher_transparent.png", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════

1 个答案:

答案 0 :(得分:1)

该文件位于lib文件夹中。因此,您需要在URL中链接lib。检查下面。

@override
  Widget build(BuildContext context) {
    return Container(
      child: Image.asset("lib/drawables/ic_launcher_transparent.png", width : 100, height: 100),
  );
}

Pubspec如下所示。

  • pubspec.yaml
    Uses-material-design:true
    资产:
    -lib / drawables /