无法用ImagePicker Flutter分配文件

问题描述 投票:0回答:1

我正在从https://pub.dev/packages/image_picker学习图像选择器,但是当我逐步使用方式时,我不明白为什么会出错。

这是问题:

首先,我声明一个File变量

File _imageFile ;

然后我在方法中使用它,

_getimg() async{
var _img = await ImagePicker(source: ImageSource.gallery);
setState(() {
      _imageFile = _img ;
    });
}

然后我收到此错误:

类型为'File的值(其中File在D:\ Flutter \ flutter \ bin \ cache \ pkg \ sky_engine \ lib \ io \ file.dart)'无法分配给'File类型的变量(其中File在D:\ Flutter \ flutter \ bin \ cache \ pkg \ sky_engine \ lib \ html \ html_dart2js.dart)'。

flutter
1个回答
1
投票

文件声明之间存在冲突。 html程序包具有一个类File的声明,而io程序包具有另一个声明(相同的名称,不同的来源)。

实际上,使用html用于Web,io用于控制台,服务器或移动应用程序,因此请检查导入并根据正在处理的项目类型删除iohtml

另一种解决方案是这样定义您的导入:

import 'package:html/html.dart' as h; //"h" can change, is just an example
import 'dart:io' as i; //"i" also can be another char or word, is just an example

//And when you need to create a File,
//you can decide if you want to create
//an io File or an html File

main(){
    i.File f1 = ...; //The io File, starting with "i."
    h.File f2 = ...; //The html File, starting with "h."
}

© www.soinside.com 2019 - 2024. All rights reserved.