Home > Net >  Unable to load asset: Image
Unable to load asset: Image

Time:04-07

I have this code for asset image

 Image.asset(
              '${listPic[1]}',
           
            ),

the images are located in a List
final List<Image> listPic= <Image>[ Image.asset('assets/images/pic1.jpg'), Image.asset('assets/images/pic2.jpg'), ];

I know the pubsec works, because when I directly type the image location like this

Image.asset(
              'assets/images/pic1.jpg',
            ),

the image is displayed, Is there something wrong with the list or Image.asset() I have tried using .toString after the ${listPic[0]} it didn't do anything instead said unnecessary string interpolation.

gives this error

The following assertion was thrown resolving an image codec: Unable to load asset: Image(image: AssetImage(bundle: null, name: "assets/images/pic1.jpg"), frameBuilder: null, loadingBuilder: null, alignment: Alignment.center, this.excludeFromSemantics: false, filterQuality: low)

CodePudding user response:

The list you declared is a List<Image> type, but you are using it as if it's a List<String>.

Try this instead:

final List<String> imagePaths = [ 
    'your/path/pic1.jpg', 
    'your/path/pic2.jpg',
];

Image.asset(
    toolImage[1],
)
  • Related