Flutter not loading images that failed to load the first time - flutter

Switch airplane on mode and navigate to a StatefulWidget that includes a NetworkImage. The image fails to load since there is no connectivity. Now switch on airplane mode off. The image doesn't load. Navigating to another view and back again also doesn't help. Minimizing the app doesn't help, but restarting it completely does.
How would you resolve this? How do I make Flutter try to reload the image connectivity is regained?

Add the flutter_image plugin and use NetworkImageWithRetry.

After 2 days of trying all image plugins and answers on stackoverflow. I realized the error goes away when I go back and come back to same screen. So I retried loading images 3 times.
CachedNetworkImage(
key: ValueKey(imageUrl
.split),
placeholder: (context, url) => Padding(
padding: EdgeInsets.all(
MediaQuery.of(context).size.width * 0.2),
child: CircularProgressIndicator()),
errorWidget: (context, url, error) => CachedNetworkImage(
key: ValueKey(imageUrlsplit +
'retry1'),
placeholder: (context, url) =>
Padding(padding: EdgeInsets.all(MediaQuery.of(context).size.width * 0.2), child: CircularProgressIndicator()),
errorWidget: (context, url, error) => CachedNetworkImage(key: ValueKey(imageUrlsplit + 'retry2'), placeholder: (context, url) => Padding(padding: EdgeInsets.all(MediaQuery.of(context).size.width * 0.2), child: CircularProgressIndicator()), errorWidget: (context, url, error) => Text(error.toString()), imageUrl: imageUrlsplit),
imageUrl: imageUrlsplit),
imageUrl: imageUrlsplit)

Related

Flutter - How to handle loading of images that may not exist

Is there any way to handle image url that not containt an image by using CachedNetworkImage package?
This is my code and when I do images loading, these errors are always popping up
CachedNetworkImage(
imageUrl: imageUrl,
placeholder: (context, url) => Center(
child: CircularProgressIndicator(
color: Theme.of(context).accentColor)),
errorWidget: (context, url, error) => BookErrorImage(),
fit: BoxFit.cover,
),
[]

Cached Network Image does not show progress indicator when image is loading

I am displaying an image using cachednetworkimage and when I click to view an image, it first shows the placeholder image and then loads the image. I don't understand why it does not show progress indicator when the image loads. I want to show progress indicator when the image loads. Incase the image is not available, then the placeholder image should be shown. What am I doing wrong here?
Container(
height: size.height * 0.35,
width: double.infinity,
child:_imageUrl != null
? CachedNetworkImage(
imageUrl: _imageUrl,
progressIndicatorBuilder: (context, url, downloadProgress) =>
Center(child:Loading()),
errorWidget: (context, url, error) => Icon(Icons.error),
)
:Image.asset('assets/images/placeholder.png'),
),
In progressIndicatorBuilder of cached network image, there's a parameter called downloadProgress, pass that value to CircularProgressIndicator to display loading.
Note: Replace NetowrkUrl with actual url
CachedNetworkImage(
height: 400.h,
imageUrl:"NetowrkUrl,
progressIndicatorBuilder: (context, url, downloadProgress) =>
Container(
margin: EdgeInsets.only(
top: 100.h,
bottom: 100.h
),
child: CircularProgressIndicator(
value: downloadProgress.progress,
color: AppColors.lightBlack)),
)
This behavior occurs because the first value of _imageUrl is null so it never goes into the CachedNetworkImage widget, it goes through the Image.asset(...) and then when the value stops being null, it goes into the CachedNetworkImage widget.
For this to work you must know the url (not null) before rendering the CacheImageNetwork widget.
An alternative solution is to replace Image.asset (...) with CircularProgressIndicator() to simulate the behavior.

resizing dynamically a CachedNetworkImage image in flutter

I want to display dynamically an image from internet in flutter according to its size
CachedNetworkImage(
fit: if (width >height) {BoxFit.fitWidth}else{BoxFit.fitHeight},
imageUrl: "url",
placeholder: (context, url) =>
CupertinoActivityIndicator(),
errorWidget: (context, url, error) =>
Icon(Icons.error),
)
I want to have width and the height of the image so I can define which fit I want to use
thanks
Maybe you just need BoxFit.contain?

Render an image widget before showing on screen

My app has a list of pages you can swipe between. These pages include network images. The problem is, the network images are fetched and built only when they are visible, meaning the user watches the whole process happen.
I understand this is the intentional functionality of flutter for efficiency, but I am looking for a way to fetch, load and cache a page (that contains network images) before the user navigates to it.
Now
Goal
In your pubspec.yaml file,
Add this as a dependency
meet_network_image:^0.1.2
In your dart file, where you want the image to be, place this line of code
MeetNetworkImage(
imageUrl:
"Your image url.jpg",
loadingBuilder: (context) => Center(
child: CircularProgressIndicator(),
),
errorBuilder: (context, e) => Center(
child: Text('Error appear!'),
),
),
just use cached_network_image package like this:
CachedNetworkImage(
imageUrl: "image path",
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => Icon(Icons.error),
),

Dynamic S3 Bucket URL Caching Flutter

I would like to cache a Network Image from an S3 bucket URL against an id or key. I tried to use the key: parameter but it didn't work. Please advise how I can achieve this. Heres the Cached Network Image code with the 'key' parameter that doesn't seem to do what I intend.
CachedNetworkImage(
key: Key(uni['university_name'].toString()), // Doesn't seem to do the job
imageUrl: uni['image_url'],
placeholder: (context, url) => CardPlaceHolder(),
errorWidget: (context, url, error) => Padding(
padding: EdgeInsets.all(21),
child: Icon(
Icons.error,
size: 30,
color: Colors.red.withOpacity(0.8),
),
),
imageBuilder: (context, imageProvider) => ...
cached_network_image has been updated to handle keys with the cacheKey value. Check out the code below:
CachedNetworkImage(
cacheKey: "my-key",
imageUrl: _url!,
fit: BoxFit.cover,
progressIndicatorBuilder:
(context, url, downloadProgress) =>
Center(
child: Platform.isAndroid
? CircularProgressIndicator()
: CupertinoActivityIndicator(),
),
)
Currently CachedNetworkImage lack cached image retrival by key
Key you used is Widgets key, not related with CachedNetworkImage behavior
Let see what is in inside of CachedNetworkImage
cached_network_image depends on flutter_cache_manager
And flutter_cache_manager has implementation (issue #179) of retrival by key but it still not released in any version
So CachedNetworkImage will miss this functionality till flutter_cache_manager version has released it new versions
Though we can use this functionality, but we need to implement widget from our side
Temporary solution
depend on flutter_cache_manager
/// this is pubspec dependencies:
flutter_cache_manager:
git:
url: https://github.com/Baseflow/flutter_cache_manager
path: flutter_cache_manager
ref: f109a48c31c4b3ed0a2c1ea2a5749c495e4fa2f4
evolve this naive implementaion to match your use case
FutureBuilder<File>(
future: DefaultCacheManager().getSingleFile(
'url', key: 'file-id'),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Image.file(snapshot.data);
}
// implement here some behavior like CachedNetworkImage does
return CircularProgressIndicator();
},
);
PS I'll update answer when CachedNetworkImage will support