简体   繁体   中英

unable to load asset [flutter]

I tried yesterday to put an image and it was working just fine but when I tried to run it again it gave me this error:

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

and here is the yaml:

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

and in main

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

so what's the problem here?

try to change this code :

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

into this one :

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

if this won't work for you then try to move your images folder inside lib folder and change your references like this :

inside yaml :

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

inside your dart code :

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

you must give full path into Image Widget like declared in your pubspec.yaml

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

appended your images file is in your app top level directory like this

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

Add full path in Main file.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM