I am trying to load images from the assets folder inside a listview item builder.
The problem is that if I format the path string with the image name it won't load the image, otherwise, if I hard code the image name flutter does manage to load the image.
I have declared the images correctly in pubspec.yaml
, a proof to that is that the image loads when I hardcode its name in the image asset path.
This is the error I am getting when trying to string format the image name to the image asset path.
Also, with vscode debugger I can see that the path is formatted successfully and contains the correct path to the image.
Code:
Widget itemItemBuilder(BuildContext context, int pos) {
Item item= StaticObjects.items[pos];
String imagePath = "assets/images/items/${item.image}.jpg";
return Card(
child: Row(
children: <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Image.asset(
imagePath,
scale: 5,
),
Text(item.name)
],
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(item.description),
Text("${item.price}₪")
],
)
],
),
);
Error:
PlatformAssetBundle.load (d:\Programs\flutter\packages\flutter\lib\src\services\asset_bundle.dart:221)
<asynchronous gap> (Unknown Source:0)
AssetBundleImageProvider._loadAsync (d:\Programs\flutter\packages\flutter\lib\src\painting\image_provider.dart:484)
AssetBundleImageProvider.load (d:\Programs\flutter\packages\flutter\lib\src\painting\image_provider.dart:469)
ImageProvider.resolve.<anonymous closure>.<anonymous closure>.<anonymous closure> (d:\Programs\flutter\packages\flutter\lib\src\painting\image_provider.dart:327)
ImageCache.putIfAbsent (d:\Programs\flutter\packages\flutter\lib\src\painting\image_cache.dart:160)
ImageProvider.resolve.<anonymous closure>.<anonymous closure> (d:\Programs\flutter\packages\flutter\lib\src\painting\image_provider.dart:325)
SynchronousFuture.then (d:\Programs\flutter\packages\flutter\lib\src\foundation\synchronous_future.dart:38)
ImageProvider.resolve.<anonymous closure> (d:\Programs\flutter\packages\flutter\lib\src\painting\image_provider.dart:323)
_rootRun (dart:async/zone.dart:1126)
_rootRun (dart:async/zone.dart:0)
_CustomZone.run (dart:async/zone.dart:1023)
_CustomZone.runGuarded (dart:async/zone.dart:925)
ImageProvider.resolve (d:\Programs\flutter\packages\flutter\lib\src\painting\image_provider.dart:315)
_ImageState._resolveImage (d:\Programs\flutter\packages\flutter\lib\src\widgets\image.dart:1010)
_ImageState.didChangeDependencies (d:\Programs\flutter\packages\flutter\lib\src\widgets\image.dart:967)
StatefulElement._firstBuild (d:\Programs\flutter\packages\flutter\lib\src\widgets\framework.dart:4376)
ComponentElement.mount (d:\Programs\flutter\packages\flutter\lib\src\widgets\framework.dart:4201)
Element.inflateWidget (d:\Programs\flutter\packages\flutter\lib\src\widgets\framework.dart:3194)
MultiChildRenderObjectElement.mount (d:\Programs\flutter\packages\flutter\lib\src\widgets\framework.dart:5551)
Anyone knows why does this happen?