How to use Image.file widget in flutter - flutter

'''
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);

Related

I am using pdf lib to generate pdf and I want to add a simple text to pdf coming from the API, the text can be very long like 3,4 pages or more

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

Flutter : image not being found [duplicate]

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

ot a stack frame from package:stack_trace, where a vm or web frame was expected

Usually stack traces that occur in asynchronous operations are cut off at the first await (asynchronous suspension).
So, I am using Chain.capture to get full traces.
Chain.capture(() {
runApp(rootWidget);
}, onError: (dynamic error, dynamic stackTrace) {
reportError(error, stackTrace);
});
However, it looks like Flutter doesn't like it and sometimes throws:
I/flutter ( 6384): The following assertion was thrown running a test (but after the test had completed):
I/flutter ( 6384): Got a stack frame from package:stack_trace, where a vm or web frame was expected. This can happen if
I/flutter ( 6384): FlutterError.demangleStackTrace was not set in an environment that propagates non-standard stack
I/flutter ( 6384): traces to the framework, such as during tests.
I/flutter ( 6384): 'package:flutter/src/foundation/stack_frame.dart':
I/flutter ( 6384): Failed assertion: line 194 pos 7: 'line != '===== asynchronous gap ===========================''
How can I fix that?

I/flutter ( 8131): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY - A non-null String must be provided to a Text widget

I got this error. How can i solve it???
Appreciate any help
I/flutter ( 8131): The following assertion was thrown building Answer(dirty):
I/flutter ( 8131): A non-null String must be provided to a Text widget.
I/flutter ( 8131): 'package:flutter/src/widgets/text.dart':
I/flutter ( 8131): Failed assertion: line 285 pos 10: 'data != null'
I/flutter ( 8131): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter ( 8131): more information in this error message to help you determine and fix the underlying cause.
I/flutter ( 8131): In either case, please report this assertion by filing a bug on GitHub:
When using Text widget, you need to provide some value.
Code like this will fail:
Text()
And code like this will pass:
Text('Some text')
Take a look inside your app where you forgot to put the value for Text widget.
If you are using variables, it might happen that value is null, and you didn't check for that.
Text(someValue == null ? '' : someValue)

Flutter play custom sound using audioplayers 0.7.7?

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