无法加载资产[颤振]

时间:2019-12-01 22:16:48

标签: android flutter dart visual-studio-code

昨天我尝试放置一个图像,但效果很好,但是当我尝试再次运行它时,出现了这个错误:

Restarted application in ٢٬٧٦٥ms.

[38;5;248m════════ Exception caught by image resource service ════════════════════════════[39;49m
[38;5;244mThe following assertion was thrown resolving an image codec:[39;49m
Unable to load asset: cat.jpg

[38;5;244mWhen the exception was thrown, this was the stack[39;49m
[38;5;244m#0      PlatformAssetBundle.load[39;49m
[38;5;244m<asynchronous suspension>[39;49m
[38;5;244m#1      AssetBundleImageProvider._loadAsync[39;49m
[38;5;244m<asynchronous suspension>[39;49m
[38;5;244m#2      AssetBundleImageProvider.load[39;49m
[38;5;244m...[39;49m
[38;5;244mImage provider: AssetImage(bundle: null, name: "cat.jpg")[39;49m
[38;5;244mImage key: AssetBundleImageKey(bundle: PlatformAssetBundle#7533e(), name: "cat.jpg", scale: 1.0)[39;49m
[38;5;248m════════════════════════════════════════════════════════════════════════════════[39;49m

这是Yaml:

flutter:
  uses-material-design: true
  assets:
    - images/cat.jpg

和主要

child: Image(
  image: AssetImage('cat.jpg'),
),

那这里有什么问题?

3 个答案:

答案 0 :(得分:0)

尝试更改此代码:

child: Image(
   image: AssetImage('cat.jpg'),
),

进入这个:

// add the full reference of the image
child: Image(
   image: AssetImage('images/cat.jpg'),
),

如果这对您不起作用,请尝试将图像文件夹移动到lib文件夹中,并更改引用,如下所示:

内部yaml:

flutter:
   uses-material-design: true
   assets:
      - lib/images/cat.jpg

在您的飞镖代码中:

//the full reference of the image
child: Image(
   image: AssetImage('lib/images/cat.jpg'),
),

答案 1 :(得分:0)

您必须像在 pubspec.yaml

中声明的那样,为图像小部件提供完整路径
child: Image(
  image: AssetImage('images/cat.jpg'),
),

图片文件添加到这样的应用顶级目录中

->(Your_App_Name)
  ->images
   ->cat.jpg

答案 2 :(得分:0)

在主文件中添加完整路径。

Image(
   image: AssetImage('images/cat.jpg'),
),