Flutter image_picker "'PickedFile'”不能分配给参数类型'文件'

carvr3hs  于 2022-11-30  发布在  Flutter
关注(0)|答案(7)|浏览(201)

我在代码中调用一个小部件,通过图像拾取器插件显示所选图像;下面是我的代码:

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

}
但是我得到了这个错误:
参数类型"'PickedFile '"不能分配给参数类型' File '
位于else语句下imageSelected上
我从图库里选了一张像这样的图片:

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

我已经定义:已选取的档案影像已选取;最后选择器= ImagePicker();
这里出什么事了?请帮帮忙...

xytpbqjk

xytpbqjk1#

Image.file()接受File类类型的属性,而ImagePicker().getImage()方法返回PickedFile类型。
我们必须使用返回的PickedFile参数的getter .path,并将该文件路径传递给create a File对象,如下所示:

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

这可以在一行中完成,如下所示:

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

在此之后,您可以使用变量imageFile,并根据需要将其像Image.file(imageFile)一样传递到Image.file()内部,或者像FileImage(imageFile)一样传递到FileImage()内部。
有关更多信息,请参见image_picker documentation on pub.dev

jhiyze9q

jhiyze9q2#

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

shstlldc3#

PickedFile imageSelected更改为File imageSelected,并使用ImagePicker.pickImage(source: ImageSource.gallery)代替picker.getImage(source: ImageSource.gallery);

dgjrabp2

dgjrabp24#

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

或者,如果您需要完整地代码:

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

x6yk4ghg5#

墨菲斯的回答是正确的。
只需将PickedFile变量路径传递给File()
示例:

final picker = ImagePicker();
    PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery);
    imageFile = File(pickedFile.path);
gblwokeq

gblwokeq6#

尝试将imageFile类型转换为PickedImage,并以类型Casting the imageFile to File返回文件,例如:- //在此处声明变量
拾取的图像图像文件;
在返回的时候:-
return图像文件(文件(图像文件路径),宽度:400,身高:400年);
我个人面临这个问题,这个解决方案解决了它。

gdx19jrr

gdx19jrr7#

试试这边...

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

相关问题