Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 597
Yasir Bucha Avatar asked Jun 24 '20 16:06

Yasir Bucha


4 Answers

//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);
  });
}
like image 44
Manthan Sutariya Avatar answered Oct 19 '22 02:10

Manthan Sutariya


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

like image 92
Morpheus Avatar answered Oct 19 '22 00:10

Morpheus


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

like image 1
JideGuru Avatar answered Oct 19 '22 02:10

JideGuru


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")
        ],
      ),
    ));
  }
}
like image 1
Zé PIKENO dos AMVS Avatar answered Oct 19 '22 00:10

Zé PIKENO dos AMVS