I know that there are a lot of thread related to this question but they didn't help.
I have an asset folder containing images. Images are properly shown in other parts of the code but not in the Alert Dialog. In the example I use a .gif file, but I had the same problem also with .png.
Error from Debug Console
════════ Exception caught by image resource service ════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: check.gif
When the exception was thrown, this was the stack
#0 PlatformAssetBundle.load
package:flutter/…/services/asset_bundle.dart:225
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync
package:flutter/…/painting/image_provider.dart:668
#2 AssetBundleImageProvider.load
package:flutter/…/painting/image_provider.dart:651
#3 ImageProvider.resolveStreamForKey.<anonymous closure>
package:flutter/…/painting/image_provider.dart:504
...
Image provider: AssetImage(bundle: null, name: "check.gif")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#234cb(), name: "check.gif", scale: 1.0)
pubspec.yaml
flutter:
uses-material-design: true
assets:
- assets/
I already tried using /assets/check.gif but I had the same problem.
Code
showDialog(
context: context,
builder:
(BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius:
BorderRadius
.all(Radius
.circular(
13)),
side: BorderSide(
color: const Color(
0xff5387ff),
width: 3)),
content: Container(
height: MediaQuery.of(
context)
.size
.height /
9,
child: Column(
children: [
Image.asset(
'check.gif',
width: 20,
height: 20,
fit: BoxFit
.contain,
),
],
),
));
});
Output:
Hope you'll be albe to help me
You need to add your folder path. Like where you store your assets that folder path and your asset name. Like the below
Image.asset(
'assets/check.gif',
width: 20,
height: 20,
fit: BoxFit
.contain,
),
Try out this, I think you forgot to add "assets" in Widget
Image.asset('assets/check.gif', width: 20,height: 20,fit:BoxFit.contain,),
Related
I am trying to add an image with a text on it through Positioned widget. But got this error while showing the image. Image Path is correct also I added the path in pubspec.yaml file.
Flutter version: 3.0.3
Complete Error
══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
The following DomException object was thrown resolving an image frame:
AbortError: Aborted by failure.
When the exception was thrown, this was the stack
Image provider: AssetImage(bundle: null, name: "assets/images/contact_us_banner.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#b62cd(), name:
"assets/images/contact_us_banner.png", scale: 1)
Code:
import 'package:flutter/material.dart';
import '../../../../constants/images.dart';
class ContactUsContainer extends StatelessWidget {
#override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
return Padding(
padding: const EdgeInsets.symmetric(vertical: 60, horizontal: 20),
child: Container(
color: const Color(0xff9ABFC7),
height: 200,
width: width * 0.80,
child: Stack(children: [
Image.asset(contactUsBanner, fit: BoxFit.contain),
const Positioned(
top: 20,
left: 20,
child: Text("Let's Build Your Website!",
style: TextStyle(
color: Colors.white,
fontSize: 40,
wordSpacing: 2,
letterSpacing: 2,
fontWeight: FontWeight.w900)))
]),
),
);
}
}
Solution:
The image size is too big (more than 50 mbs).
Just compressed the file(image) size and restart your App.
I've created a flutter package and images are inside images folder.
flutter:
uses-material-design: true
# To add assets to your package, add an assets section, like this:
# assets:
assets:
- images/
- images/location_pointer.png
I'm trying to load this image:
Widget build(BuildContext context) {
return Scaffold(
body: Image.asset(
'images/location_pointer.png',
width: 22.0,
height: 44.0,
fit: BoxFit.fill,
));
}
I've created a project and importing a package inside it:
dependencies:
flutter:
sdk: flutter
abc_pkg:
path: /Users/mosh/Documents/flutter proj/abcPackage/abc_pkg
I'm able to load the package when run this project but not able to load the images inside the package.
======== Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/location_pointer.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:675:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "images/location_pointer.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#092c6(), name: "images/location_pointer.png", scale: 1.0)
====================================================================================================
Make sure all your images are saved inside a directory called 'images', which is inside a directory called 'assets'.
Update your pubspec.yaml as follows:
flutter:
uses-material-design: true
# To add assets to your package, add an assets section, like this:
# assets:
assets:
- assets/images/location_pointer.png
Update your dart file as follows:
Widget build(BuildContext context) {
return Scaffold(
body: Image.asset(
'assets/images/location_pointer.png',
width: 22.0,
height: 44.0,
fit: BoxFit.fill,
));
}
please try this code. and make an image folder in the same
directory where you have the lib folder
in your pubspec.yaml file
flutter:
uses-material-design: true
assets:
- images/
and in your dart file
Widget build(BuildContext context) {
return Scaffold(
body: Image.asset(
'images/location_pointer.png',
width: 22.0,
height: 44.0,
fit: BoxFit.fill,
));
}
Exception caught by image resource service
(
height: 300,
width: double.maxFinite,
child: TabBarView(controller: _tabController, children: [
ListView.builder(
itemCount: 3,
itemBuilder: (BuildContext context, int index) {
return Container(
width: 200,
height: 300,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.white,
image: DecorationImage(
image: AssetImage(
"images/levender.jpg",
),
fit: BoxFit.cover,
),
),
);
When the exception was thrown, this was the stack
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/internal/js_dev_runtime/private/ddc_runtime/errors.dart 236:49 throw
packages/flutter/src/services/asset_bundle.dart 224:55 load
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 45:50
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1362:47 _rootRunUnary
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1265:19 runUnary
...
Image provider: AssetImage(bundle: null, name: "images/levender.jpg")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#0645c(), name: "images/levender.jpg", scale: 1)
It will happen sometimes. sometimes when you add an asset if this error happened just close your emulator and run it again and it will be ok.
Now I am using CircleAvatar to show a background image:
CircleAvatar(
radius: 20,
backgroundColor: Colors.transparent,
backgroundImage: foregroundImage.image,
),
but now it always show this error:
======== Exception caught by image resource service ================================================
The following _Exception was thrown resolving an image codec:
Exception: Invalid image data
When the exception was thrown, this was the stack:
#0 _futurize (dart:ui/painting.dart:5275:5)
#1 ImageDescriptor.encoded (dart:ui/painting.dart:5143:12)
#2 instantiateImageCodec (dart:ui/painting.dart:1999:60)
<asynchronous suspension>
Image provider: NetworkImage("https://static.poemhub.top/2021/5/18/laitn.github.io-favicon.ico", scale: 1.0)
Image key: NetworkImage("https://static.poemhub.top/2021/5/18/laitn.github.io-favicon.ico", scale: 1.0)
====================================================================================================
To fix this problem, I tried this way:
final Image defaultImage = Image.asset('images/Icon-App-83.5x83.5#3x.png');
var foregroundImage = counter.value.favIconUrl == "" ? defaultImage : Image.network(
global.staticResourceUrl + "/" + counter.value.localIconUrl,
loadingBuilder: (context,child,loadingProgress)=>(loadingProgress == null) ? child : CircularProgressIndicator(),
errorBuilder: (context, error, stackTrace) => defaultImage,
);
if image fetch failed or other error, set a default image. But still not fix this problem. I figure out maybe the image think this url icon correct, but CircleAvatar render failed. Is it possible to check the image valid before render using CircleAvatar? what should I do to avoid this problem? How to know the image could render successful? I also have tried to set both foreground picture and background picture like this:
CircleAvatar(
radius: 20,
backgroundColor: Colors.transparent,
foregroundImage: foregroundImage.image,
backgroundImage: defaultImage.image,
),
but it looks like this:
both have background and foreground picture.
bool isErrorOccured = false;
CircleAvatar(
radius: 36,
child: isErrorOccured?Image.asset(""):Image.network(""),
onBackgroundImageError: (_, __) {
setState(() {
isErrorOccured = true;
});
},
),
or
SizedBox(
height: height,
width: height,
child: ClipRRect(
child: networkImageUrl == ''
? Image.asset(assetImageUrl, fit: BoxFit.cover)
: CachedNetworkImage(
fit: BoxFit.cover,
imageUrl: networkImageUrl,
placeholder: (context, url) => const Center(
child: CupertinoActivityIndicator(),
),
errorWidget: (context, url, error) => Container(
color: Colors.grey[300],
child: const Icon(
Icons.error,
color: Colors.red,
),
),
),
borderRadius: BorderRadius.circular(height / 2),
))
I am trying to use the flutter_svg package to load some .svg icon.
class _MyHomePageState extends State<MyHomePage> {
final String iconPath = "assets/icons/adept.svg";
...
}
I added the assets/ folder containing the icons/ folder to the pubspec.yaml file:
assets:
- assets/
And when I try to load the icon inside my body:
body: Center(
child: Container(
child: LimitedBox(
child: SvgPicture.asset('iconPath', color: Colors.black, width: 100, height: 100,),
maxHeight: 100,
maxWidth: 100,
)
)
),
I get this StackTrace, Unable to load asset: assetName,
I/flutter (10154): ══╡ EXCEPTION CAUGHT BY SVG ╞═══════════════════════════════════════════════════════════════════════
I/flutter (10154): The following assertion was thrown resolving a single-frame picture stream:
I/flutter (10154): Unable to load asset: iconPath
I/flutter (10154):
I/flutter (10154): When the exception was thrown, this was the stack:
I/flutter (10154): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:223:7)
...
I/flutter (10154): Picture provider: ExactAssetPicture(name: "iconPath", bundle: null, colorFilter: null)
I/flutter (10154): Picture key: AssetBundlePictureKey(bundle: PlatformAssetBundle#153a9(), name: "iconPath",
I/flutter (10154): colorFilter: null)
I/flutter (10154): ════════════════════════════════════════════════════════════════════════════════════════════════════
Kindly explain to me what am I missing?
Did you change like below code?
assets:
- assets/
- assets/icons/
body: Center(
child: Container(
child: LimitedBox(
child: SvgPicture.asset(iconPath, color: Colors.black, width: 100, height: 100,),
maxHeight: 100,
maxWidth: 100,
)
)
),
just do 3 work:
Run this command in terminal : flutter clean.
and in android studio go to file and select:
Invalidated caches and restarted.
then get dependencies again by this command: flutter pub get