0

I am trying to load an image with this code

CircleAvatar(
    child: Image(
    image: getImage(snapshot.value['img']),
    ),
),

but if the image is not founded I want to replace it with a default image and I try this code

AssetImage getImage(String image) {
    AssetImage img;
    try {
        img = AssetImage('images/${widget.categoria}/$image.png');
    } catch (e) {
        img = AssetImage('images/non_disp_big.png');
    }
    return img;
}

I allready add all images dependencies in pubspec.yaml I just wanna replace the image with a default if not found it in the folder

This is the error:

════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: images/produzioni-tipiche/biplano.png

When the exception was thrown, this was the stack: 
#0      PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
<asynchronous suspension>
#1      AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:484:44)
#2      AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:469:14)
#3      ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:flutter/src/painting/image_provider.dart:327:17)
...
Image provider: AssetImage(bundle: null, name: "images/produzioni-tipiche/biplano.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#15081(), name: "images/produzioni-tipiche/biplano.png", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
Logemann
  • 2,767
  • 33
  • 53
ari.asapp
  • 1
  • 2

2 Answers2

0

you should just return a string for the image asset inside your CircleAvatar widget like the code below:

CircleAvatar(
            child: Image(
          image: AssetImage(getImage(snapshot.value['img'])),
        ))

the method returns a string now:

String getImage(String image) {
    String img;
    try {
      img = 'images/${widget.categoria}/$image.png';
    } catch (e) {
      img = 'images/non_disp_big.png';
    }
    return img;
  }
hewa jalal
  • 952
  • 11
  • 24
  • How can I check if this asset exist in my asset folder? – ari.asapp May 01 '20 at 17:14
  • take a look at [this](https://stackoverflow.com/a/55025228/11385677). – hewa jalal May 01 '20 at 17:39
  • I need something that return a string and not a Future, for now I had resolved removing data without an asset – ari.asapp May 02 '20 at 14:06
  • in question you said to load default image when the asset image is not available which you can do that by `io.File(path).exists()` in an if statements as if it returns true then the file exists now you can set the image to that file and if it returns false now you can use the default image. – hewa jalal May 02 '20 at 14:41
  • I have sobstituted the "path" with 'images/${widget.categoria}/$image.png' but return always false – ari.asapp May 03 '20 at 15:11
0

I know this is an old question, but the best way is user errorBuilder function that Image.asset provides and load for alternative widget.

 CircleAvatar(
      child: Image.asset(getImage(snapshot.value['img']),
      errorBuilder: ((context, error, stackTrace) => const Image.asset('images/non_disp_big.png')),
    )),
BYISHIMO Audace
  • 585
  • 1
  • 8
  • 18