I am trying to load an array of images inside a horizontal ListView.builder
from an API fetching data from the server. The API returns an array inside another with the key profile_img
.Below is what the API return looks like:
[
{
"id": "1",
"profile_img": [
[
"https://website.com/image1.png",
"https://website.com/image2.png"
]
],
"vendor_tax_check": "Registered",
"total_list": "Total bookings 3 in the next 8 days"
}
{
"id": "2",
"profile_img": [
[
"https://website.com/image1.png",
"https://website.com/image2.png"
]
],
"vendor_tax_check": "Registered",
"total_list": "Total bookings 13 in the next 30 days"
}
]
I got an error Image provider: NetworkImage("[https://website.com/image1.png, https://website.com/image2.png]", scale: 1.0)
and Image key: NetworkImage("[https://website.com/image1.png, https://website.com/image2.png]", scale: 1.0)
from the key profile_img
above when I implemented in listview as seen below:
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: data == null ? 0 : data.length,
itemBuilder: (BuildContext context, int index) {
return Column(
child:children[
Text(data[index]['id']),
ListView.builder(scrollDirection: Axis.horizontal,
itemCount:data == null ? 0 :data[index]["profile_img"].length,
itemBuilder: (BuildContext context, int index) {
return Align(
alignment: Alignment.centerRight,
child:Container(
child:Image.network(data[index]["profile_img"].toString()),
));
},
),
]
);
},)
At one point, I tried to add an index
like => Image.network(data[index]["profile_img"][index].toString()
above but it only displays one of the images and the other shows error RangeError (index): Invalid value: Only valid value is 0: 1
. Please can anyone kindly help on how to solve this?
2
Answers
So I finally solved this myself and feel I should post the solution here in case anyone faces a similar issue, below is my solution:
The value for the key
profile_img
is :To get the first image, you need to do :
profile_img[0][0]
and notprofile_img[0]
as it is an array in an array. You can then doprofile_img[0][1]
to get the second image etc.You added that you tried with :
Image.network(data[index]["profile_img"][index].toString()
. But you forgot a [0] so it should be :Image.network(data[index]["profile_img"][0][index].toString()
.I hope that can help
EDIT :
Here is a fixed code wrapping every image in an array into a column