繁体   English   中英

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

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

我正在尝试使用image_picker获取图像,然后传递给image_cropper 我采取了一些不同的方法来避免在图像选择后返回主屏幕,然后再去裁剪图像屏幕。

这是我的图像选择和图像裁剪代码。

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.

在尝试过Null Safety ,但随后抛出此错误:

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

我的代码与 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;
}

请建议我一个更好的方法来做我想做的事情。

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

我认为你应该先拿起img,检查它是否有效。 然后将路径传递给 imageCropper。 所以上面的代码应该可以正常工作..

暂无
暂无

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

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