-1

To preface, I have reviewed the post here, but am still having trouble.

I am building a container that is decorated with an image, only if that image exists in the assets/images/ folder. If it does not exist, the container is decorated with a default image instead.

The image path is formatted string using a variable, image_id, that assumes a String response from an API.

Here is a code / pseudo-code hybrid of what I'm trying to do...

Container(
  height: 200,
  width: 300,
  decoration: BoxDecoration(
    image: DecorationImage(
      image: (AssetImage('assets/images/${image_id}.jpg') exists) //pseudo-code here
        ? AssetImage('assets/images/${image_id}.jpg')
        : AssetImage('assets/images/default.jpg'),
      fit: BoxFit.cover,
    ),
  ),
);
Braiam
  • 1
  • 11
  • 47
  • 78
tap
  • 11
  • 1
  • 5
  • 1
    How can you be in a position where the image does not exist in assets? Assets is not a folder that is populated dynamically / programmatically. It is up to you, when you build the app (at which time the contents of assets folder is fixed for your app), to ensure all the necessary assets (that you refer to in your code) exist. – GrahamD Oct 28 '20 at 15:56
  • Correct. And because the assets folder is static, there are instances in which an image_id is passed-in, but an image of that ID does not exist in the assets/images folder. In those cases, the image at path 'assets/images/default.jpg' is returned. – tap Oct 28 '20 at 15:59
  • 1
    Why would your app be receiving image ids that are not in your assets folder?? You seem to be trying to use your assets folder as a database. I think you should review your logic. – GrahamD Oct 28 '20 at 16:14
  • 1
    You seem to be coding to handle when you forgot to set something up. Very strange. – GrahamD Oct 28 '20 at 16:15
  • I will edit my question for added clarity. image_id is an API response. – tap Oct 28 '20 at 17:04
  • Everything is correct here: but how to show a default asset image, which is present, or an image saved localy. e.g. in an user profile view? The saved image has priority. – cwhisperer Apr 07 '21 at 16:55

3 Answers3

0

You should know if the asset exists as you're the responsible for adding those.

One option you have is by using: FlutterGen This library will create variables, method, etc for all your assets, if the variable is null, then, there you go.

You can also try to get the image like this:

final assetImage = Image.asset('path/to/asset.jpg');

and then check if that image is null or not. Even, if you want, you can take that and pre cache your image like this:

await precacheImage(assetImage.image, context);
Mariano Zorrilla
  • 7,165
  • 2
  • 34
  • 52
  • most of the time the file path are dynamic strings coming from the api response, i.e. path = 'assets/image/${user.initials}' – Tremmillicious Feb 03 '22 at 09:50
  • 1
    that's incorrect. If you're using local assets to then being used by an API call, then you need to rethink your logic as it's a complete waste of local space from the binary build. If you're using an API, get the image from a server directly. – Mariano Zorrilla Feb 03 '22 at 14:08
-2

You can list all of the assets. Then you can check if certain asset is exist or not.

easeccy
  • 4,248
  • 1
  • 21
  • 37
-5

You are responsible for adding assets. Thus you will know if it's there or not.

Arpit Awasthi
  • 493
  • 2
  • 8
  • 1
    The image_id is a string returned by an API call. I do not have assets for all the possible API responses. – tap Oct 28 '20 at 17:03
  • most of the time the file path are dynamic strings coming from the api as the above poster say – Tremmillicious Feb 03 '22 at 09:47
  • I have a database, that's where the name of the file comes... the client has to choose one of the assets... but as the image name comes from the database, sometimes the data is not correct, thats why i want to check if the file exists on the App. – Jesus Garcia May 12 '22 at 18:57