Yasir Bucha
Yasir Bucha

Reputation: 223

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

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..

Upvotes: 18

Views: 30415

Answers (7)

Mani Baluchamy
Mani Baluchamy

Reputation: 51

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

Upvotes: 0

user15459383
user15459383

Reputation: 1

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.

Upvotes: 0

Tiny
Tiny

Reputation: 409

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

Upvotes: 1

Ramon
Ramon

Reputation: 106

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")
        ],
      ),
    ));
  }
}

Upvotes: 1

Manthan Sutariya
Manthan Sutariya

Reputation: 19

//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);
  });
}

Upvotes: 2

Morpheus
Morpheus

Reputation: 361

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

Upvotes: 26

JideGuru
JideGuru

Reputation: 7660

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

Upvotes: 1

Related Questions