I do not know how many images I will have, whether there are any methods such as "assets.length" or other ways
ListView.builder(
itemCount: // here I have to specify how many images I have in my resource file
scrollDirection: Axis.horizontal,
shrinkWrap: true,
itemBuilder: (BuildContext context, int index) {
return Container(
width: 100,
margin: EdgeInsets.only(right: 25),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
image: DecorationImage(
fit: BoxFit.cover,
image: AssetImage(
'assets/four_season/hotel${index}.jpg',
),
),
),
);
},
),
I will create many hotels, and it is unknown how many images there will be
3
Answers
You can try this code block to work with,
Just call the function with proper path, if you select path for your case,
assets
then the file count will be 10 (four_season’s 4 & marriot_hotel’s 6 files).or if you select path
assets/four_season
then the count will be 4.create a Class for assets:
and use as following:
make sure to update the assets list when you add new images.
If you need to inspect what assets are available to your app at run time, the listAssets API is probably what you’re looking for.
You can call this API, filter the resulting list however you please, and then inspect the
length
on the list.Full sample application that lists all available assets: