How to handle exceptions in FadeInImage.assetNetwork - flutter

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 ?

Related

How to solve NetworkImageLoadException in 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('😒');
},

Unsupported Error was thrown while handling a gesture. Unsupported operation: indexed set || flutter error

I am working on a list of chip widget where the parent widget is ListView builder.
When I click on any of the chip I got this error The following UnsupportedError was thrown while handling a gesture:
Unsupported operation: indexed set
When the exception was thrown, this was the stack:
I am working on flutter web
here is my code:-
ChipList(
style: TextStyle(color: primaryBlue),
shouldWrap: true,
listOfChipNames: _dogNames,
// extraOnToggle: (index) {
// print(index);
// },
borderRadiiList: const [5],
activeBgColorList: const [Colors.white],
inactiveBgColorList: const [Colors.white],
activeTextColorList: const [Colors.white],
listOfChipIndicesCurrentlySeclected: const [
100
],
inactiveBorderColorList: [primaryBlue],
activeBorderColorList: [primaryBlue],
),
I'm not really familiar with ChipList but looking at the documentation of it it says an important warning message
⚠ Warning ! The flutter_lints package might suggest that you make
listOfChipIndicesCurrentlySeclected a constant.
PLEASE DO NOT DO SO. The widget will not respond to user interaction,
seemingly without any reason.
That list will be updated as the user interacts with the widget. It is
intended to be used in case you would like to incorporate the contents
for any logic.
I don't know if it's related to your issue, but I recommend removing the const keyword that you have at your listOfChipIndicesCurrentlySeclected

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),
)

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,
),
),