'Flutter unable to load image assets (Loading images through ListView.builder)
I tried to load from the assets/images
folder using Listview.builder()
but it keeps throwing this error:
EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/images/image_1.png
I have also tried the flutter clean
method, but to no avail.
Project structure is
lib
|- home.dart
|- main.dart
assets
|- images
|- image_1.png
|- image_2.png
|- ...
|- image_10.png
pubspec.yaml
home.dart
class MyHomePage extends StatelessWidget {
// --snip--
@override
Widget build(BuildContext context) {
return Scaffold(
// --snip--
body: Listview.builder(
itemBuilder: (context, index) =>
Image.asset('assets/images/image_${index + 1}.png', fit: BoxFit.fitWidth),
itemCount: controller.imageCount,
),
// --snip--
)
}
}
pubspec.yaml
flutter:
use-material-design: true
assets:
- assets/images/
Edit:
Screenshot of project directory from / (I'm not allowed to embed images yet)
Screenshot of project directory from /assets/images/ And here in, lies my mistake.
Solution 1:[1]
itemCount: controller.imageCount.length
and
- assets/images/image_1.png
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gregorio Avila |