How to solve NetworkImageLoadException in flutter - flutter

I am trying to fetch image using image.network(), not getting result, showing error, actually image address will get in api responses,
code look like this,
Container(
height: double.infinity,
width: MediaQuery.of(context).size.width *0.18,
child: Image.network(
value.recentObservations[index]
["thumbnail"],),)
and error showing like this,
════════ Exception caught by image resource service ════════════════════════════
The following NetworkImageLoadException was thrown resolving an image codec:
How tom resolve this error, comment your references

This is because your server is not allowing to fetch the image url data. So resolve this from server end.
And you can use
errorBuilder
in network image to show any default image when your image gets failed to load into specified widget.
You can use below sample code for errorBuilder.
Image.network(
path.image,
width: 50,
height: 50,
errorBuilder: (BuildContext context, Object exception,
StackTrace? stackTrace) {
return const Text('😒');
},

Related

cached_network_image throw Invalid image data exception if url is 404

I am using CachedNetworkImage in book list. If image url is wrong or 404, it is throw Invalid image data exception in firebase Crashlytics report. But in app, image in list is showing black container that in written in errorWidget. My question is how I can catch this error instead of throwing exception. You can even get the exception at debug mode.
Now firebase is keeping on sending the email alert, because of repetitive crashes.
CachedNetworkImage(
width: 110.0,
fit: BoxFit.cover,
imageUrl: ebook.thumbnail,
placeholder: (context, url) => Container(color: Colors.black12),
errorWidget: (context, url, error) =>
Container(color: Colors.black12),
)

Exception Flutter from fetch network Image without connection internet

after turning off the internet i gets this error: "Remove network images from cache on any exception during loading"
my app fetch pictures from api. how can i fix this bug how i prevent that?
return Column(children: [
GestureDetector(
child: Image.network(thumbnailUrl))])
You can use the Cached Network Image Plugin if you are developing for android and ios. In Web, the plugin is not working as expected.
Use the following as a widget for your application.
import 'package:cached_network_image/cached_network_image.dart';
Widget commonCacheImageWidget(String? url, {double? width, BoxFit? fit, double? height}) {
if (url!.startsWith('http')) {
return CachedNetworkImage(
placeholder: (context, url) => Image.asset('assets/path....', fit: BoxFit.cover),
imageUrl: url,
height: height,
width: width,
fit: fit,
);
} else {
return Image.asset(url, height: height, width: width, fit: fit);
}
}
In Placeholder you can have any widget, here am using a local image that is stored as an asset.
Or You can check whether the internet connection is there or not before loading the image and change that to some other widget.

How to handle exceptions in FadeInImage.assetNetwork

I have a FadeInImage.assetNetwork to display picture. I want to handle the exception when the image is not found. According to the documentation, one should use imageErrorBuilder:
A builder function that is called if an error occurs during image
loading.
If this builder is not provided, any exceptions will be reported to
[FlutterError.onError]. If it is provided, the caller should either
handle the exception by providing a replacement widget, or rethrow the
exception.
Then I use it like this:
FadeInImage.assetNetwork(
image: myImageUrl,
placeholder: "assets/images/myPlaceholder.gif",
imageErrorBuilder: (context, error, stackTrace) {
return Image.asset("assets/images/loadFailedImage.png",
width: 100, height: 100);
},
)
When my image is not found, loadFailedImage.png is well displayed instead but the exception is not caught, contrary to what the documentation says. How can I handle it to avoid that exception to stay uncaught ?

How to solve exception caught by widgets library?

I want to use cached network image to display images in my flutter app, however it shows this:
═══════ Exception caught by widgets library ═══════════════════════════════════
Invalid argument (onError): Error handler must accept one Object or one Object and a StackTrace as arguments.: Closure: () => Null
The relevant error-causing widget was
CachedNetworkImage
lib/widgets/custom_image.dart:5
════════════════════════════════════════════════════════════════════════════════
custom_image.dart -
import 'package:flutter/material.dart';
Widget cachedNetworkImage(String mediaUrl) {
return CachedNetworkImage(
imageUrl: mediaUrl,
fit: BoxFit.cover,
placeholder: (context, url) => Padding(
child: CircularProgressIndicator(),
padding: EdgeInsets.all(20.0),
),
errorWidget: (context, url, error) => Icon(Icons.error),
);
}
Please help.
After doing some research and trial and error.
I think the issue is in the package. The error you are getting was a
bug in the old version of the HTTP package and I think your
cached_network_image is outdated.
Try flutter pub upgrade, resolve any
dependency conflicts, do flutter clean, and flutter pub get.
Also, make sure your flutter SDK is updated as well.
Wrap your CachedNetworkImage with a Container.
return Container(
width:...,
height:...,
child: CachedNetworkImage(..)));

FadeInImage with unknown image at runtime

I have a FadeInImage in my app, with the image loaded in response to a user action. This means at runtime, I dont know the image, and just want the FadeInImage to not display anything.
If I pass in null for the image, or an empty string, I get a runtime error.
flutter: ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
flutter: The following ArgumentError was thrown resolving an image codec:
flutter: Invalid argument(s): No host specified in URI file:///
Here it the relevant Widget declaration:
Container(
height: 100,
child:FadeInImage.memoryNetwork(
placeholder: kTransparentImage,
image: _d2apiModel.emblemUrl,
fit: BoxFit.fitWidth,
),
(where _d2apiModel is a Provider which is updated based on user actions).
I know I could put a default image in the asset bundle, copy it out of the bundle to the doc storage, get the file URI for the image, and use that as the default but 1) that seems like overkill, 2) i run into some async issues.
It seems like I am missing something obvious, but I cant figure out the approach. Any suggestions would be appreciated.
You can show a regular Image with kTransparentImage while your provider hasn't been updated, and then show your FadeInImage when it updates:
Container(
height: 100,
child: hasProviderBeenUpdated
? Image.memory(
kTransparentImage,
fit: BoxFit.fitWidth,
)
: FadeInImage.memoryNetwork(
placeholder: kTransparentImage,
image: _d2apiModel.emblemUrl,
fit: BoxFit.fitWidth,
),
),