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

Viewed 18937

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?

4 Answers

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