Exception caught by widgets library null check to xfile to file - flutter

When I build after changing Xfile to file, it says null check operator user on a null value, but I don't know what the problem is
The code is too long, so I'll just write the gist
please help me......
XFile? _imageFile;
File file = File(_imageFile!.path);

Are you picking any file in XFile or just declaring it?
Then of course you're going to get null value
Otherwise,
Add a check when you've picked a file from user
XFile? imageFile;
imageFile = //Your File Picking logic
if(imageFile != null) {
File _file = File(imageFile!.path);
}
Else you can also try reading the bytes if in any case your file path is inaccessible and create a new file object
imageFile!.readAsBytes();

What does a null check operator?
This operator (!) throws an exception when a nullable variable wasn't initialized (so, it's null).
How do I fix the error?
Since in your code the XFile? _imageFile variable is nullable (indicated by the ?), probably it turns out to be actually null when being assigned to a File object.
A quick solution the null check problem could be this:
late File file;
XFile? _imageFile;
if (_imageFile != null) {
file = File(_imageFile!.path);
}
But in your exact code, what you should really do is initialize _imageFile, as right now you are just declaring it.
XFile? _imageFile = await ImagePicker().pickImage(source: ImageSource.gallery); //Example with image_picker package

Related

The name 'XFile' isn't a type so it can't be used as a type argument. Try correcting the name to an existing type, or defining a type named 'XFile'

[here is the issue (https://i.stack.imgur.com/D0zPF.png)
I have tried to run flutter clean and flutter pub get but no changes I don't know what causing this issue.
I will appreciate your help guys.
Could you try this please:
chooseFile() async {
final chosenFile = await ImagePicker().pickImage(source: ImageSource.gallery);
if(chosenFile != null) {
return chosenFile.path;
}
}
If it does not work could you please show me the code you've tried before and explain the desired results.

How to add File to Array (List) :Flutter

How to add File to Array (List)
I got an image from ImagePicker
var image = await ImagePicker.pickImage(source: imageSource);
_images are declared by followed
List<File> _images = List<File>();
I tried as Follows
_images = image as List<File>
But I got errors like this
Unhandled Exception: type '_File' is not a subtype of type
'List' in type cast
You're getting the error because Future<File> pickImage(..) returns File not List<File>.
In you code you're trying to cast File as List<File>:
_images = image as List<File>
If you only want to store the File returned from Future<File>pickImage(..), then you can add the File to the List
_images.add(image);
Also Future<File>pickedImage(..) method is deprecated so please try using Future<PickedFile> getImage(..), which returns PickedFile instead of File.

Is it possible to change the location of sorted video of image_picker plugin?

I am picking video from camera in this way. and using image_picker: ^0.6.7+22 plugin.
final _picker = ImagePicker();
Future<String> recordAndGetVideo()async{
PickedFile file = await _picker.getVideo(source: ImageSource.camera);
if(file != null){
return file.path;
}
return null;
}
After record ad video it stored the video and final path is this path. /storage/emulated/0/Android/data/com.example.app/files/Pictures/a190e227-a42c-4b09-bad8-4a9591454ff64584234230431626592.mp4
Now is it possible to store that video is different dir ? like /storage/emulated/0/Example App/a190e227-a42c-4b09-bad8-4a9591454ff64584234230431626592.mp4
Yes it is possible, But you have to re-write the file from one location to another.
This is the only way.

What is the proper way to load images in Flutter and handle exceptions on load

I'd like to know what is the proper way to load images from the device, and handle exceptions if the image is missing or corrupted. The user picking an image from the device and I want to open it. I'm using the image later in my code, so it is not enough for me just to show it in some widget.
Currently, I'm using the following code that works fine in most cases:
Future<ui.Image> imageLoadFromDevice(String path) async {
await askPermissionForStorage();
ImageProvider imageProvider = FileImage ( File ( path ), scale: 1 );
Completer<ImageInfo> completer = Completer();
imageProvider.resolve(ImageConfiguration()).addListener(ImageStreamListener((ImageInfo info, bool _) {
completer.complete(info);
}));
ImageInfo imageInfo = await completer.future;
return imageInfo.image;
}
But if the image is missing or corrupted, there is a print in the console "Exception caught by image resource service", but my exception catcher above this function not getting the exception.
Do I loading the image properly, or is there a better way?
In case this code is OK, how should I catch exceptions, particularly missing file or corrupted image?
I'm not sure about corrupted images, but in the case of missing files, you can store the File(path) in a variable and use varname.exists() to check before setting your ImageProvider.
Good question! :)
You can use a simpler code below, that loading an image just as you want.
import 'dart:ui' as ui;
Future<ui.Image> imageLoadFromDevice(String path) async {
await askPermissionForStorage();
File fileImage = File(path);
final Uint8List imageUint8List = await fileImage.readAsBytes();
final ui.Codec codec = await ui.instantiateImageCodec(imageUint8List);
final ui.FrameInfo frameInfo = await codec.getNextFrame();
return frameInfo.image;
}
To catch exceptions from this code you can put it inside try-catch. Any exception while loading the image, should produce a general exception that can be caught and handled.
To handle errors from an ImageStream, use the onError argument to the ImageStreamListener constructor:
imageProvider.resolve(ImageConfiguration()).addListener(ImageStreamListener(
(ImageInfo info, bool _) { }, // called for success
onError: (Object error, StackTrace? stackTrace) { }, // called for error
));

How to Save set image of PickedFile type to a image in Flutter?

I want to set get image from the camera and set it to the following code:
image: DecorationImage(image: FileImage(file),
So I did this:
File file = await ImagePicker.pickImage(
source: ImageSource.camera,
);
Now above code works fine for now, but 'pickImage' is deprecated and we should use imagePicker.getImage() method instead.
So I used imagePicker.getImage() instead:
PickedFile file = await ImagePicker().getImage(
source: ImageSource.camera,
);
But when I try to set this file to the following image type, it doesn't work
image: DecorationImage(image: FileImage(file),)
How to solve this issue?
You need to use path property of picked file
image: DecorationImage(image: FileImage(File(file.path)),)
If someone might struck on a similar issue
change from pickImage to getImage and still needs a File.
PickedFile selectedFile = await ImagePicker().getImage(source: source);
File selected = File(selectedFile.path);
I was stuck in a similar type of problem where
await ImagePicker.pickImage( source: ImageSource.camera, );
was deprecated, and I have to use
await ImagePicker().getImage( source: ImageSource.camera, );
but the problem raised when I wanted to save it,
so if anyone stuck in suck kind of the problem I used this hack
final savedImage = File(imageFile.path).copy('${appDir.path}/$fileName');
this is one part of my code...
PickedFile to File
If your app needs dart:io File objects to operate, you may transform PickedFile to File like so:
final pickedFile = await _picker.getImage(...);
final File file = File(pickedFile.path);