【问题标题】:type 'Null' is not a subtype of type 'File' of 'function result' in flutter?类型“Null”不是颤振中“函数结果”的“文件”类型的子类型?
【发布时间】:2021-12-17 13:37:17
【问题描述】:

我正在努力将图像上传到 SQL 服务器,但出现错误:

类型“Null”不是“函数结果”的“文件”类型的子类型

这是我初始化文件的方式:

late File _image;

final picker=ImagePicker();

Future choiceImage() async{
 var pickedImage= await picker.getImage(source: ImageSource.gallery);
 setState(() {

  _image=File(pickedImage!.path);
});
}
 Future choice2Image() async{
   var pickedImage= await picker.getImage(source: ImageSource.camera);
  setState(() {
  _image=File(pickedImage!.path);
});
 }    

并通过以下方式获取此图像:

var pic =await http.MultipartFile.fromPath("image", _image.path);

不知道是什么问题?

【问题讨论】:

  • 参考我的回答herehere 希望对你有帮助

标签: flutter dart


【解决方案1】:

ImagePicker 如果您没有从图像选择器表中选择任何图像,则可以返回空值。因此,您需要处理空值。

这是你可以做的。

File? _image;

final picker=ImagePicker();

Future choiceImage() async{
 var pickedImage= await picker.getImage(source: ImageSource.gallery);
   if(pickedImage!=null)
     {
       setState(() 
          {
           _image=File(pickedImage!.path);
         });
      }
    }

【讨论】:

    【解决方案2】:

    而不是使用late File _image; 您可以尝试使用:

    File? _image;
    

    【讨论】:

    • 使用这个后我得到一个错误“null check operator used on null value”
    • 您只能使用File? _image
    • 你的意思只是文件? _image 不带分号“;”
    • 不不,编辑了答案。
    猜你喜欢
    • 2022-08-23
    • 1970-01-01
    • 2021-11-20
    • 2021-12-25
    • 2021-10-08
    • 2022-07-16
    • 2021-02-25
    • 2021-08-12
    • 2020-09-29
    相关资源
    最近更新 更多