![](/img/trans.png)
[英]How to implement library image_picker and image_cropper to a class that extends Stateless widget?
[英]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.