【问题标题】:File cant be assigned with ImagePicker Flutter无法使用 ImagePicker Flutter 分配文件
【发布时间】:2020-04-20 12:53:47
【问题描述】:

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

这就是问题所在:

首先,我声明一个文件变量

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)' 不能 分配给类型为“文件”的变量(其中文件定义在 D:\Flutter\flutter\bin\cache\pkg\sky_engine\lib\html\html_dart2js.dart)'。

【问题讨论】:

    标签: flutter


    【解决方案1】:

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

    事实上,html 用于 Web,io 用于控制台、服务器或移动应用程序,因此请检查您的导入并删除 io 或 html,具体取决于您正在处理的项目类型.

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

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

    【讨论】:

    • 当我想为imagepicker声明文件时,我必须使用哪个,io或html?
    • io 文件。 image_picker 仅适用于移动设备,因此请继续使用 :-)
    猜你喜欢
    • 2021-01-13
    • 2021-10-13
    • 2021-11-06
    • 2021-06-10
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多