Exception caught by image resource service, The following assertion was thrown resolving an image codec: - flutter

I am trying to load an image into a scroll, but every time it does not load the image due to error:
Exception caught by image resource service The following assertion was thrown resolving an image codec:
Unable to load asset: assets/images/jorge.png
When the exception was thrown, this was the stack:
0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:464:44)
2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:449:14)
...
Image provider: AssetImage(bundle: null, name: "assets/images/jorge.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#dd41f(), name: "assets/images/jorge.png", scale: 1.0)
(2) Exception caught by image resource service
Unable to load asset: assets/images/heitor.png
(3) Exception caught by image resource service
Unable to load asset: assets/images/john.png
I/chatty ( 8763): uid=10206(com.festapp.flutter_app_fest) identical 5 lines
E/AccessibilityBridge( 8763): VirtualView node must not be the root node.
flutter clean was used, besides exchanging images
final User currentUser = User(
id: 0,
name: 'Current User',
imageUrl: 'assets/images/greg.png'
);
//usuários
final User Jorge = User(
id: 1,
name: 'Jorge',
imageUrl: 'assets/images/jorge.png'
);
final User John= User(
id: 2,
name: 'John',
imageUrl: 'assets/images/john.png'
);
final User Heitor = User(
id: 3,
name: 'Heitor',
imageUrl: 'assets/images/heitor.png'
);
final User Gui = User(
id: 4,
name: 'Gui',
imageUrl: 'assets/images/heitor.png'
);
and images use in:
return Padding(
padding: EdgeInsets.all(10.0),
child: Column(
children: <Widget>[
CircleAvatar(
radius: 35.0,
backgroundImage: AssetImage(favorites[index].imageUrl),
),
SizedBox(
height: 6.0,
),
Text(favorites[index].name, style: TextStyle(
color: Colors.blueGrey,
fontSize: 16.0,
fontWeight: FontWeight.w600,
),
),
],
),
);
Exception caught by image resource service
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/images/jorge.png
When the exception was thrown, this was the stack:
0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:464:44)
2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:449:14)
...
Image provider: AssetImage(bundle: null, name: "assets/images/jorge.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#dd41f(), name: "assets/images/jorge.png", scale: 1.0)
(2) Exception caught by image resource service
Unable to load asset: assets/images/heitor.png
(3) Exception caught by image resource service
Unable to load asset: assets/images/john.png

It seems that these image files aren't in your project:
assets/images/jorge.png
assets/images/john.png
assets/images/heitor.png
Make sure you are doing these things:
Add the images in the folder assets/images/
Specify the images in pubspec.yaml
Execute flutter packages get / tap Packages get from
pubspec.yaml / tap Get dependencies from any dart file.
Use Image.asset() or AssetImage() to show them.

i also faced the same issue,
the solution is to stop the main.dart -by clicking the red button given in taskbar
and then restart it.
sometime its not able to load the images when you add it as fresh

To solve this error, you should check your pubspec.yaml file, either the asset folder is indexed like below or not:
flutter:
[2 whitespaces or 1 tab]assets:
[4 whitespaces or 2 tabs]- assets/images/ #index all files inside images folder
[4 whitespaces or 2 tabs]- assets/images/elephant.jpg #index only elephant.jpg

Related

Loading image in Flutter | Path from content root doesn't work, but absolute path works

Very simple app, the image is in project/images folder.
When I try to load it from the relative path:
'images/diamond.png'
the image doesn't load.
When I do it from the absolute path: '/Users/MacBookAir/StudioProjects/secondtry_iamrich/images/diamond.png'
it does.
Why is this? Can anyone please help?
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.blueGrey,
appBar: AppBar(
title: const Text('I am Rich'),
backgroundColor: Colors.blueGrey[900],
),
body: const Center(
child: Image(
image: AssetImage('images/diamond.png'),
),
),
),
),
);
}
This is the error it is providing:
======= Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/diamond.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:237:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:658:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "images/diamond.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#99cb5(), name: "images/diamond.png", scale: 1.0)
====================================================================================================
Do not forgot to add image folder path to your pubspec.ymal folder like
assets:
- assets/images/
- assets/images/icons/
- assets/loader.json
Use this plugin in your android studio it will auto generate your all assets no need to write path.
Use it like this
Image.asset(Assets.imagesNoDataFound, scale: 3.5)
Uncomment and change the asset section in your pubspect.yaml file can fix it.
assets:
- images/

type 'Null' is not a subtype of type 'List<int>' in type cast in flutter

I am having a strange problem. I don't think that there is any problem with the url of image. Problem happens when I restart the simulator
Update
It seems the the image is expired
How can I catch this exception?
I am just calling a network image
return CircleAvatar(
radius: 40.0,
backgroundImage: NetworkImage(
sessionControl.currentUser.value.picture),
backgroundColor: Colors.transparent,
);
Error log
Launching lib/main.dart on iPhone 13 in debug mode...
Xcode build done. 13.5s
Connecting to VM Service at ws://127.0.0.1:57758/38wgT4iPnbk=/ws
flutter: Check 1 : Is Amplify SetState set to true ? false
[GETX] Instance "GetMaterialController" has been created
[GETX] Instance "GetMaterialController" has been initialized
flutter: Check 1 : Is Amplify SetState set to true ? true
flutter: Session Sign In Status = true
flutter: Check 1 : Is Amplify SetState set to true ? true
[GETX] Instance "HomeController" has been created
[GETX] Instance "HomeController" has been initialized
flutter: Requested for pulling current user info
[GETX] Instance "SessionController" has been created
[GETX] Instance "SessionController" has been initialized
flutter: Printing session control picture :
flutter: Printing session control picture :
flutter: Printing session control picture : https://dontak4d751b4dd50548f1b4819e95919c2d17220339-oxlnewenv.s3.ap-southeast-2.amazonaws.com/public/Aaronimage_picker_29FADB72-2761-4BB9-89B0-9EFA849305BA-33481-0000009746CE94EE.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=%2F20210930%2Fap-southeast-2%2Fs3%2Faws4_request&X-Amz-Date=20210930T195105Z&X-Amz-Expires=17999&X-Amz-SignedHeaders=host&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEHQaDmFwLXNvdXRoZWFzdC0yIkYwRAIgJGnlBRuLiHfiSRjwIH8XnKYWqrmIPMfdOcchb8P878sCIBmI362NDDXenQUm%B8i30UG9uy0kucILDuxd3RreSTpb5HBJyUtJrvViYzy45XuHWjJ1WKEDxeAZdiDHEZPubnkkcGkcj0jdcaXtBP2OpR1cPT5MTWyv1dJ7mobezeRWvteSVzygA4Ot%2Fe5Yrf2ARLA3JDiIenmaxX7%2FAhvOUb0ppL9BocsAv0wojTSsdPvvQooRLqr%2FSjzEpDrVkHf7QOI50im9eBlk%2F8dKKERfwjIP41dyv1Wo5hyIc%2BfUmsXml0nRnluB<…>
flutter: Note: calling the network image
════════ Exception caught by image resource service ════════════════════════════
The following _CastError was thrown resolving an image codec:
type 'Null' is not a subtype of type 'List<int>' in type cast
When the exception was thrown, this was the stack
#1 NetworkImage._loadAsync
<asynchronous suspension>
(elided one frame from dart:async)
Image provider: NetworkImage("https://dontak4d751b4dd50548f1b4819e95919c2d17220339-oxlnewenv.s3.ap-southeast-2.amazonaws.com/public/Aaronimage_picker_29FADB72-2761-4BB9-89B0-9EFA849305BA-33481-0000009746CE94EE.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=%2F20210930%2Fap-southeast-2%2Fs3%2Faws4_request&X-Amz-Date=20210930T195105Z&X-Amz-Expires=17999&X-Amz-SignedHeaders=host&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEHQaDmFwLXNvdXRoZWFzdC0yIkYwRAIgJGnlBRuLiHfiSRjwIH8XnKYWqrmIPMfdOcchb8P878sCIBmItfJeejulE3fb0a7FgzSkedEkb8CrRxq%2BgeT47%2BJlaWUzJm6rC4r1%2F8Bc7T9aDqtYmNy1xlfVKr2D776gLFlWc3uMbwWeK3QQIRgb%2F0dMv4%2Bam4Z763as4v65nrs26%2B9S9U0XuII3V4M7pXDIspQwj%2B9Mq6sTsFwbYx0DMeCjVI96WAJdxuACAKJIgHR9DVaIHWI77IogWieRJPIH12quhZ%2F4GZr%2BfWvh8OL8Ak8feHTtcy%2FEQndGukUianBFEv2Erl7fc7VLdkLUZK1D40wa8Qlk1uH6Kt%2B36H%2FQNEIYRd%2FpzoyvSMuuhtDJ%2FfkQyvRv7TvJbCDzwibKom34sZyjK5yVc%3D&X-Amz-Signature=9537750a0fe8d32358f5461f305539147cbba13a1d337ee34b44f263d926da43", scale: 1.0)
Image key: NetworkImage("https://dontak4d751b4dd50548f1b4819e95919c2d17220339-oxlnewenv.s3.ap-southeast-2.amazonaws.com/public/Aaronimage_picker_29FADB72-2761-4BB9-89B0-9EFA849305BA-33481-0000009746CE94EE.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=%2F20210930%2Fap-southeast-2%2Fs3%2Faws4_request&X-Amz-Date=20210930T195105Z&X-Amz-Expires=17999&X-Amz-SignedHeaders=host&X-Amz-Security-Token=IQoJb3JpZ2luX2VjHBhOUPrHu25bSB2fUAlzezITPYaksWbJyrZD%2BTiUzRvD5T2CWSDifjtYYLJ9HVj06XupHwhNyNrAC3wiIG%2Fsy1c4%2FYIGKUQ%2BR8PO7le%2FyNNMVQdFGFJvzW9BBhgcwR6JQwAUpHYpFiWZ1ZeVmk0fTR%2FFU721Tc9M%2BsVdxhcDnZVOR4CtIMR5jbNKSRp3ambx75lgZJEXwQiKol0aSTDx6iGlKkKBPCucT4JlCu9m5wlAZSU8ejJbWbgWxxK6sP1rLVQTC2Erl7fc7VLdkLUZK1D40wa8Qlk1uH6Kt%2B36H%2FQNEIYRd%2FpzoyvSMuuhtDJ%2FfkQyvRv7TvJbCDzwibKom34sZyjK5yVc%3D&X-Amz-Signature=9537750a0fe8d32358f5461f305539147cbba13a1d337ee34b44f263d926da43", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════
If I update the profile pic then it loads but after sometime it doesn't with above error. Can't understand why pls help.
You can use onBackgroundImageError to catch error
CircleAvatar(
radius: 40.0,
backgroundImage: NetworkImage(
sessionControl.currentUser.value.picture,
),
onBackgroundImageError: (exception,context) {
print('${sessionControl.currentUser.value.picture} Cannot be loaded');
print('Error msg : ${exception.toString()}');
},
backgroundColor: Colors.transparent,
),
and you can check if the provided URL working or not :
CircleAvatar(
radius: 40.0,
backgroundImage: NetworkImage(
'https://via.placeholder.com/120',
),
onBackgroundImageError: (exception,context) {
print('${sessionControl.currentUser.value.picture} Cannot be loaded');
},
backgroundColor: Colors.transparent,
),

Unable to load Image asset exception thrown in Flutter

Here's my code:
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.blueGrey,
appBar: AppBar(
title: Text('I AM RICH'),
backgroundColor: Colors.blueGrey[900],
),
body: Center(
child: Image(
image: AssetImage('images/diamond.png'),
),
),
),
),
);
}
THIS IS THE EXCEPTION I AM GETTING.
My pubspec.yaml file follows proper indentation.
======= Exception caught by image resource service ================================================
The following assertion was thrown resolving an image codec:
Unable to load asset: images/diamond.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:672:14)
<asynchronous suspension>
Image provider: AssetImage(bundle: null, name: "images/diamond.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#a2375(), name: "images/diamond.png", scale: 1.0)
====================================================================================================
I assume that your image is under the assets folder. So, you need to call image like this:
AssetImage('assets/images/diamond.png')
And be sure that you wrote this correctly in your pubspec.yaml:
assets:
- assets/images/
make changes in your pubspec.yaml file
add this line to import all the files present in images folder
assets:
- images/
for showing image you can use any one of the code shown below
Image(image: AssetImage('images/diamond.png')),
or
Image.asset('images/diamond.png'),
Add asset path in pubspec.yaml as
assets:
- images/diamond.png
or you can also add parent folder path
assets:
- images/
Here's my pubspec.yaml file:
name: i_am_rich
description: Show off your wealth.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
uses-material-design: true
assets:
- images\
Ultimately the problem is with typing mistake, "images" should be "images/"
Interesting how pub get executes with an exit code of 0 even after typing error.

Image does not show on leading part of list tile when using Circle Avatar and back ground image

I am building this app and just have a small UI problem. I am using a ListView builder. On the leading part of the tile, I want a circular icon to display with the desired image.
I believe my implementation is correct but when I run my code the circle avatar does come out on the leading part of the tile but it does not display the desired image. Instead, it just shows a blue circle on the leading end of each tile.
I am also getting an exception from the terminal. I will attach my code and the exception that I am getting. I would really appreciate suggestions from anyone. Thank you all!
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
List<String> titles = ["Covid-19 Cases", "Covid-19 Vaccine Tracker",
"Dr. John Campbell Youtube Channel", "Medcram YouTube Channel"];
List<String> files = ["cases.png", "vaccineIMG.png", "dr.johncampbell.png",
"Medcram.png"];
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.black54,
appBar: AppBar(
backgroundColor: Colors.redAccent,
title: Text("Your Covid-19 Briefing"),
),
body: ListView.builder(
itemCount: titles.length,
itemBuilder: (context, index) {
return Card(
child: ListTile(
onTap: () {
_launchUrl(index);
},
title: Text(
titles[index],
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
letterSpacing: 1.0,
color: Colors.black,
),
),
leading: CircleAvatar(
backgroundImage: AssetImage('assets/${files[index]}'),
),
),
);
}
),
),
);
}
_launchUrl(index) async {
List<String> _urlsToLaunch = [
"https://www.worldometers.info/coronavirus/",
"https://www.nytimes.com/interactive/2020/science/coronavirus-vaccine-tracker.html",
"https://www.youtube.com/c/Campbellteaching/videos/",
"https://www.youtube.com/c/Medcram/videos"
];
if (await canLaunch(_urlsToLaunch[index])) {
await launch(_urlsToLaunch[index]);
} else {
throw "Could not open $_urlsToLaunch";
}
}
}
Error:
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/cases.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:664:31)
#2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:648:14)
#3 ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:501:13)
...
Image provider: AssetImage(bundle: null, name: "assets/cases.png")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#e7274(), name: "assets/cases.png", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by image resource service ════════════════════════════════════════════════
Unable to load asset: assets/vaccineIMG.png
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by image resource service ════════════════════════════════════════════════
Unable to load asset: assets/dr.johncampbell.png
════════════════════════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by image resource service ════════════════════════════════════════════════
Unable to load asset: assets/Medcram.png
════════════════════════════════════════════════════════════════════════════════════════════════════
pubspec.yaml file:
name: coronavirus_news_updates
description: A new Flutter application.
# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
url_launcher: ^5.4.11
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.3
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
Have you included your assets in your pubspec.yaml file?
flutter:
assets:
- assets/your_image_name.png
or to incude the entire folder:
flutter:
assets:
- assets/

Flutter/Dart copyCrop Function Exception: Could not instantiate image codec

I want to display a cropped Image in Flutter. I use the Image.memory Widget. When I use the orginal image there is no Problem.
var tempStore = await ImagePicker.pickImage(source: ImageSource.camera);
List<int> byteListe = await tempStore.readAsBytes();
image = dartimage.Image.fromBytes(400, 400, byteList);
But after I used the copyCrop() Function I get an Exception..
https://pub.dev/documentation/image/latest/image/copyCrop.html
cropImage = dartimage.copyCrop(image, 4, 0, 100, 100);
Expanded(
child: Container(
child: cropImage != null ? Image.memory(cropImage.getBytes()): Text('No Image'),
),
),
This is the Exception!
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following _Exception was thrown while resolving an image:
Exception: Could not instantiate image codec.
When the exception was thrown, this was the stack:
#0 _futurize (dart:ui/painting.dart:4304:5)
#1 instantiateImageCodec (dart:ui/painting.dart:1682:10)
#2 PaintingBinding.instantiateImageCodec (package:flutter/src/painting/binding.dart:88:12)
#3 MemoryImage._loadAsync (package:flutter/src/painting/image_provider.dart:714:18)
#4 MemoryImage.load (package:flutter/src/painting/image_provider.dart:706:14)
...
Image provider: MemoryImage(_Uint8ArrayView#a573e, scale: 1.0)
Image configuration: ImageConfiguration(bundle: PlatformAssetBundle#c13af(), devicePixelRatio: 4.0, locale: de_DE, textDirection: TextDirection.ltr, platform: android)
Image key: MemoryImage(_Uint8ArrayView#a573e, scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════