Reputation: 419
In my flutter app I am storing the URLs from firebase storage inside Firestore. I am expecting there to be null URL's as it is dependent on the user uploading images. In my code I am checking for null values, but for some reason the code still tries to load the null values. How do I correctly prevent flutter from loading the null values in network image. My code for the load is as follows:
Container(
width: 150,
height: 150,
child: ListView.builder(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.all(8),
itemCount: photoList.length,
itemBuilder: (BuildContext context, int index) {
print(photoList[index]);
return photoList[index] != null ? Image.network(photoList[index]) : Container(height:400,child: Text('No Images to Display'));
}
),
),
Debug give the following for the print above when there is no image:
I/flutter (27038): url - null
When there is a image then there is a proper URL and the image loads without any issue.
So not quite sure why Flutter is still trying to load the null even though in that case of null I would like a text box instead
Upvotes: 0
Views: 1387
Reputation: 7716
It looks like the value of photoList[index]
is not consistent. As it prints "url - null" when it's expected to contain a null value and when it's not null, it contains an actual url that loads an image.
You can the either of the following:
Ensure your null images are actually null instead of "url - null" and the null check will work.
Use the errorBuilder property of the Image.network widget to display your error widgets like this:
Image.network(photoList[index],
errorBuilder: (context, error, stackTrace) {
return Container(height:400,child: Text('No Images to Display'));
}
)
Upvotes: 2
Reputation: 87
What type of data stores in photoList
? If String
there is a chance "null" value is actually a string but not null
.
Upvotes: 2