This question already has answers here:
How to add image in Flutter
(15 answers)
Closed 1 year ago.
This is my widget to display a little pic of myself in the app.
Widget image() {
return CircleAvatar(
radius: 50.0,
backgroundImage: AssetImage('images/me.jpeg'),
);
}
Yet, for some reason, I keep getting this
The following assertion was thrown resolving an image codec:
Unable to load asset: images/me.jpeg
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:225:7)
<asynchronous suspension>
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:668:31)
#2 AssetBundleImageProvider.load (package:flutter/src/painting/image_provider.dart:651:14)
#3 ImageProvider.resolveStreamForKey.<anonymous closure> (package:flutter/src/painting/image_provider.dart:504:13)
...
Image provider: AssetImage(bundle: null, name: "images/me.jpeg")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#14309(), name: "images/me.jpeg", scale: 1.0)
Although the image is inside the folder. Please help
make sure you added the images to pubspec.yaml file and then run pub get
assets:
- images/me.jpeg
Related
I am using pdf lib to generate pdf and I want to add a text in pdf coming from the API, the text can be very long like 3,4 pages or more. Below is my code.
List<pw.Widget> widgets = [];
widgets.add(pw.Text(provider.getExtractedText));
final pdf = pw.Document();
pdf.addPage(
pw.MultiPage(
build: (pw.Context context) {
return [
pw.Wrap(
children: widgets
)
];
},
),
);
but i am getting the following exception.
E/flutter (19405): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: This widget created more than 20 pages. This may be an issue in the widget or the document. See https://pub.dev/documentation/pdf/latest/widgets/MultiPage-class.html
E/flutter (19405): #0 MultiPage.generate.<anonymous closure> (package:pdf/src/widgets/multi_page.dart:251:11)
E/flutter (19405): #1 MultiPage.generate (package:pdf/src/widgets/multi_page.dart:255:8)
E/flutter (19405): #2 Document.addPage (package:pdf/src/widgets/document.dart:118:10)
E/flutter (19405): #3 _DocumentConversionScreenState.saveFile (package:speak_and_translate/screens/document_conversion_screen.dart:215:11)
E/flutter (19405): <asynchronous suspension>
E/flutter (19405):
Your stacktrace gives you file and line number - so go and look there:
// Detect too big widgets
if (sameCount++ > maxPages) {
throw Exception(
'This widget created more than $maxPages pages. This may be an issue in the widget or the document. See https://pub.dev/documentation/pdf/latest/widgets/MultiPage-class.html');
}
As you see, it compares to maxPages. Where is that set? Check line 152:
this.maxPages = 20,
So, change that default to a sensible number for your application:
pw.MultiPage(
maxPages: 40, // <- add max pages here
build: (pw.Context context) {
This is the code I am using.
I have checked it with different file format but still showing the same error, i have checked the "img.path" its showing the path to the image. I don't understand why I am getting an error.
String targetPath;
var result = await FlutterImageCompress.compressAndGetFile(
img.path,
targetPath,
quality: 88,
format: CompressFormat.webp,
);
Error I am getting:
[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: The method 'toLowerCase' was called on null.
Receiver: null
Tried calling: toLowerCase()
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5)
#1 Validator.checkFileNameAndFormat
package:flutter_image_compress/src/validator.dart:19
#2 FlutterImageCompress.compressAndGetFile
package:flutter_image_compress/flutter_image_compress.dart:150
#3 _AddImageState.uploadFile
package:softclaw/…/addpost/addimage.dart:225
#4 _AddImageState.build.<anonymous closure>
package:softclaw/…/addpost/addimage.dart:97
<asynchronous suspension>
targetPath is the one that goes through the checkFileNameAndFormat > toLowerCase, therefore check to ensure this is not null.
'''
I/flutter ( 7954): The following FileSystemException was thrown resolving an image codec:
I/flutter ( 7954): Cannot open file, path = 'image.png' (OS Error: No such file or directory, errno = 2)
I/flutter ( 7954):
I/flutter ( 7954): When the exception was thrown, this was the stack:
I/flutter ( 7954): #0 _File.open.<anonymous closure> (dart:io/file_impl.dart:366:9)
I/flutter ( 7954): (elided 13 frames from dart:async)
I/flutter ( 7954): ...
I/flutter ( 7954):
I/flutter ( 7954): Path: image.png
I/flutter ( 7954): ════════════════════════════════════════════════════════════════════════════════════════════════════
I am using image.file to display the widget but it throws the below exception how to solve that and I add my code as image.what is the major difference between image.asset widget and image.file widget.Thanks in advance.
You need to add the correct location of the file:
File file = new File('directoryLocation/image.png');
Another way to load images, is to add them to the assets folder in the project, then in the pubspec.yaml, you can do the following:
flutter:
assets:
- assets/my_icon.png
- assets/background.png
Then do:
Widget build(BuildContext context) {
return Image(image: AssetImage('assets/my_icon.png'));
}
Check the docs:
https://flutter.dev/docs/development/ui/assets-and-images
Image.asset is used to load images from your projects assets folder. As because its already available in the project and pretty straight foreword to use.
Image.asset('assets/image.jpg');
On the other hand Image.file is used to load images from devices internal/external storage. This way you have to locate the image using ImagePicker or any other library to get the path of the image. Using this path create a File object and provide it to Image.file.
String path = 'your/image/path/here';
File imageFile = File(path);
Widget image = Image.file(imageFile);
Flare animation won't display on the screen.
I'm using:
Container(
height: 300.0,
width: 300.0,
child: FlareActor('checkmarkone.flr',
fit: BoxFit.contain,
alignment: Alignment.center,
),
),
When I'm running the app I get this message in the run console:
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: Unable to load asset: checkmarkone.flr
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
<asynchronous suspension>
#1 FlareCacheAsset.load (package:flare_flutter/flare_cache_asset.dart:31:20)
#2 Cache.getAsset (package:flare_flutter/cache.dart:56:15)
#3 cachedActor (package:flare_flutter/flare_cache.dart:35:16)
#4 FlareRenderBox.loadFlare (package:flare_flutter/flare_render_box.dart:322:35)
#5 FlareActorRenderObject.coldLoad (package:flare_flutter/flare_actor.dart:322:20)
#6 FlareRenderBox.load (package:flare_flutter/flare_render_box.dart:272:7)
#7 FlareRenderBox.attach (package:flare_flutter/flare_render_box.dart:115:7)
#8 AbstractNode.adoptChild (package:flutter/src/foundation/node.dart:132:13)
#9 RenderObject.adoptChild (package:flutter/src/rendering/object.dart:1238:11)
#10 RenderObjectWithChildMixin.child= (package:flutter/src/rendering/object.dart:2905:7)<…>
I appreciate all suggestions.
Thanks
Make sure you have checkmarkone.flr file in your assets folder, then add it to your pubspec.yaml file
flutter:
assets:
- checkmarkone.flr
and then, run flutter packages get command to update the dependencies. Finally, do a cold restart of your project
Please check your pubspec.yaml file and ensure that the file checkmarkone.flr has been added/loaded correctly.
Eg.
pubspec.yaml
... (more on top)
# 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
# To add assets to your application, add an assets section, like this:
assets:
- assets/checkmarkone.flr # Add it here
... (more on bottom)
Now wait for the flutter task (updating packages) to finish, then try loading the Flare animation using the asset path added above in your screen / widget via hot restart.
widget.dart
Container(
height: 300.0,
width: 300.0,
child: FlareActor('assets/checkmarkone.flr',
fit: BoxFit.contain,
alignment: Alignment.center,
),
),
Further reading
https://flutter.dev/docs/development/ui/assets-and-images
pubspec.yaml
flutter:
uses-material-design: true
assets:
- assets/Images/1.png
- assets/Images/MP3.mp3
Test.dart
Widget localAsset() {
return _tab([
Text("Click to play"),
_btn('Play', () => audioCache.play('assets\Images\MP3.mp3')),
]);
}
I am new to flutter, for my applications i want play two sounds mode(background sound ,button action sound), after referred from flutter package i have changed code like as above , when i used this widget in my material,i am getting below error,
E/flutter ( 2750): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error: Unhandled exception:
E/flutter ( 2750): Unable to load asset: assets/assetsImagesMP3.mp3
E/flutter ( 2750): #0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:221:7)
Backslash are Windows-specific. Use slashes instead. Android is Unix-based and so is iOS
audioCache.play('assets/Images/MP3.mp3')