Flutter image_picker "'PickedFile'" can't be assigned to the parameter type 'File'
Asked Answered
L

7

18

I'm calling a widget in my code to display the selected image through image-picker plugin; following is my code:

Widget _imagePlaceHolder() {
if (imageSelected == null){
  return Text("No File Selected!!");
} else {
  Image.file(imageSelected, width: 400, height: 400);
}

}

but I'm getting this error:

The argument type "'PickedFile'" can't be assigned to the parameter type 'File'

on imageSelected under else statement.

I'm picking an image like this from gallery:

Future _openGallery(BuildContext context) async {
var picture = await picker.getImage(source: ImageSource.gallery);
this.setState(() {
  imageSelected = picture;
});}

I've defined: PickedFile imageSelected; final picker = ImagePicker();

what's going wrong here? Please help..

Lonna answered 24/6, 2020 at 16:5 Comment(0)
F
26

Image.file() accepts a property of type File class, whereas the ImagePicker().getImage() method returns a type PickedFile.

We have to utilise the getter .path of the returned PickedFile argument and pass that file path to the create a File object as follows:

void _setImage() async {
    final picker = ImagePicker();
    PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery);
    imageFile = File(pickedFile.path);
}

This may be done in one line as follows:

void _setImage() async {
    imageFile = File(await ImagePicker().getImage(source: ImageSource.gallery).then((pickedFile) => pickedFile.path));
}

After this, you can use the variable imageFile and pass it inside Image.file() like Image.file(imageFile), or FileImage() like FileImage(imageFile) as required.

For more, see the image_picker documentation on pub.dev

Falsework answered 27/6, 2020 at 17:6 Comment(1)
This is an answer for mobile. How to do it with web?Monsour
B
2
//many time when user import dart.html package than it throw error so keep note that we have to import dart.io

import 'dart.io';


final imagePicker = ImagePicker();
File imageFile;

Future getImage() async {
  var image = await imagePicker.getImage(source: ImageSource.camera);
  setState(() {
    imageFile = File(image.path);
  });
}
Bavardage answered 22/8, 2020 at 7:42 Comment(0)
S
1

Change PickedFile imageSelected to File imageSelected and use ImagePicker.pickImage(source: ImageSource.gallery) instead of picker.getImage(source: ImageSource.gallery);

Sauls answered 24/6, 2020 at 16:9 Comment(2)
still doesn't work.... if i do the suggested change then it gives error on 'picture' in my code.. the error is: A value of type 'PickedFile' can't be assigned to a variable of type 'File'. Try changing the type of the variable, or casting the right-hand type to 'File'.dartinvalid_assignmentLonna
You can use the classic ImagePicker.pickImage(source: ImageSource.gallery). i updated my answerSauls
F
1

Morpheus answer is correct.

Just pass in the PickedFile variable path to File().

Example:

final picker = ImagePicker();
    PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery);
    imageFile = File(pickedFile.path);
Furgeson answered 27/6, 2020 at 17:34 Comment(0)
L
1
import 'package:image_picker/image_picker.dart';
import 'dart:io';  

  var image;
      void imagem() async {
        PickedFile picked = await ImagePicker().getImage(
            preferredCameraDevice: CameraDevice.front, source: ImageSource.camera);
        setState(() {
          image = File(picked.path);
        });
      }

Or case you need of code full:

    import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class Photos extends StatefulWidget {
  @override
  _PhotosState createState() => _PhotosState();
}

class _PhotosState extends State<Photos> {
  var image;
  void imagem() async {
    PickedFile picked = await ImagePicker().getImage(
        preferredCameraDevice: CameraDevice.front, source: ImageSource.camera);
    setState(() {
      image = File(picked.path);
    });
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
        child: Center(
      child: Column(
        children: [
          RaisedButton(
            onPressed: imagem,
            child: Text("Imagem"),
          ),
          image != null ? Image.file(image) : Text("I")
        ],
      ),
    ));
  }
}
Luella answered 26/9, 2020 at 12:58 Comment(0)
K
0

Try converting your imageFile type to PickedImage and return the file in type Casting the imageFile to File, Like:- // Declaring the variable here

PickedImage imageFile;

And at the time of returning:-

return Image.file(File(imageFile.path),width: 400,height: 400,);

I personally faced this problem, and this solution solved it.

Kellyekellyn answered 23/3, 2021 at 9:23 Comment(1)
Please use text formatting options in StackOverflow editor. Proper formatting helps to read your answer easily.Gid
F
0

Try this way...

 Future pickImageFromGallery() async {
    try {
      final pickedFile = await picker.pickImage(
    source: ImageSource.gallery, 
      );
      setState(() {
    widget.imageFile = File(pickedFile!.path);
      });
      if (pickedFile == null) {
    throw Exception('File is not available');
      }
    } catch (e) {
      print(e);
    }
Friedrick answered 27/11, 2022 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.