简体   繁体   English

处理来自 image_cropper flutter 中 image_picker 的 null 文件?

[英]Handling null file from image_picker in image_cropper flutter?

I'm trying to get an image using image_picker package and then passing to image_cropper .我正在尝试使用image_picker获取图像,然后传递给image_cropper I have taken a bit different approach to avoid getting back to the home screen after image selection before going to crop image screen.我采取了一些不同的方法来避免在图像选择后返回主屏幕,然后再去裁剪图像屏幕。

Here is my code for image selection and image crop.这是我的图像选择和图像裁剪代码。

Future<File> getImageFromGallery(BuildContext context) async{
    final File croppedImage = await ImageCropper.cropImage(
        sourcePath: File((await ImagePicker().getImage(source: ImageSource.gallery)).path).path,
        maxWidth: 1080,
        maxHeight: 1080,
        aspectRatio: CropAspectRatio(ratioX: 1.0, ratioY: 1.0)
    );

    if (croppedImage  != null) {

      return croppedImage;
    }
    return null;
}
Error: The getter 'path' was called on null.

In tried Null Safety , but then it throws this error:在尝试过Null Safety ,但随后抛出此错误:

Failed assertion: line 81 pos 12: 'await File(sourcePath).exists()': is not true.

My code with Null Safety.我的代码与 Null 安全。

Future<File> getImageFromGallery(BuildContext context) async{

    final File croppedImage = await ImageCropper.cropImage(
        sourcePath: File((await ImagePicker().getImage(source: ImageSource.gallery)).path).path,
        maxWidth: 1080,
        maxHeight: 1080,
        aspectRatio: CropAspectRatio(ratioX: 1.0, ratioY: 1.0)
    );

    if (croppedImage  != null) {

      return croppedImage;
    }
    return null;
}

Please suggest me a better way to do what I'm trying to do.请建议我一个更好的方法来做我想做的事情。

var img = await ImagePicker().getImage(source: ImageSource.gallery);
final File croppedImage = await ImageCropper.cropImage(
    sourcePath: img.path,
    maxWidth: 1080,
    maxHeight: 1080,
    aspectRatio: CropAspectRatio(ratioX: 1.0, ratioY: 1.0)
);

I think you should pick up the img first, check if it's valid img or not.我认为你应该先拿起img,检查它是否有效。 And then pass the path to imageCropper.然后将路径传递给 imageCropper。 So the code above should work fine..所以上面的代码应该可以正常工作..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM