FadeInImage with unknown image at runtime - flutter

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

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('😒');
},

Is there any way to get around 404 on Image.network?

I'm stuck with this for about 3 hours, and after a lot of research still can't do it.
FadeInImage(
image: NetworkImage(
"http://url.test/content/dam/images/aaf/products/_codepageff/${name}.png"),
height: 20,
placeholder: const AssetImage('assets/wait.png'),
imageErrorBuilder: (context, error, stackTrace) {
return Image.asset('assets/ups.png',
fit: BoxFit.fitWidth);
},
fit: BoxFit.fitWidth,
),
I've tried A lot of codes, and still without no success on 404. Is there a known error or am I missing anything?
Also I'm building this inside a StatelessWidget class.
Give absolute url for NetworkImage. 404 means that the image is not found from that url.

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.

FadeInImage.assetNetwork requires absolute path. Relative path for placeholder is not working. (in flutter)

I am displaying image through FadeInImage.assetNetwork which requires two members : image and placeholder.
ClipRRect( //forcing image to rounded corners
borderRadius: BorderRadius.only(
topLeft: Radius.circular(15),
topRight: Radius.circular(15),
),
child: FadeInImage.assetNetwork(
image: imageUrl,
placeholder: '/Users/aayush/Documents/Flutter/Apps/meals_app/assets/images/food-placeholder.png', //here full path is necessary. relative path is not working.
// placeholder: '../../assets/images/food-placeholder.png', //this relative path is not working.
height: 250,
width: double.infinity,
fit: BoxFit.cover, //will resize and crop the image
),
),
Here is my folder structure :
Currently I am at lib/widgets/meal_item.dart and the placeholder image is situated at assets/images/food-placeholder.png.
so, my relative path will be : ../../assets/images/food-placeholder.png .
the error message when I try to use relative path here :
════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: ../../assets/images/food-placeholder.png
When the exception was thrown, this was the stack
#0 PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:224
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:672
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "../../assets/images/food-placeholder.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#df521(), name: "../../assets/images/food-placeholder.png", scale: 1.0)
I tried it many times but it is not working only in this widget. relative path is working perfectly in other widgets.
Please give me solution for this or suggestion so that I can display placeholder image till the url image is fetched.
Here is the full screenshot :
For relative path, create path like this
'assets/images/food-placeholder.png'
And ensure that you have added images path in assets in pubspec.yaml like this
assets:
- assets/images/
You have to specify image location in Pubspec.yaml file.
Thats how you add assets to your flutter project.
After that you can use image as below:
FadeInImage.assetNetwork(
image: imageUrl,
placeholder: 'assets/images/food-placeholder.png',
height: 250,
width: double.infinity,
fit: BoxFit.cover,
),
After adding image path to pub spec you have to Stop currently running session and than rebuild or re-run the App.
Try this commands.
flutter clean
flutter pub get
flutter run

Invalid image data error: how to display image in Flutter app using aws s3 url?

I'm using this code to display image:
Container(
child: Image(
image: CachedNetworkImageProvider(
"https://zyle.s3.amazonaws.com/IMG_1_v%2BjwOGN%2BK6FmX2wA.png")),
);
But it's showing the error:
Try this:
Image.network("https://zyle.s3.amazonaws.com/IMG_1_v%2BjwOGN%2BK6FmX2wA.png")