I’m using the following code to select and load an image. Is there any way to validate if the image returned is valid? For example, users pick up video files instead of image files in which case an exception should be raised.The issue is that the Exception block never gets called even if the return image is invalid.
Future<Image?> imgFromGallery(
double cWidth, double cHeight ) async {
final picker = ImagePicker();
final XFile? pickedImage = await picker.pickImage(
source: ImageSource.gallery,
maxWidth: cHeight,
maxHeight: cHeight,
imageQuality: 100);
if (pickedImage == null) return Future.value();
try {
Image? img = Image.file(File(pickedImage.path));
if (img == null) //this won't work, always false
return Future.value();
return img;
} catch (e) {
return null; //this will never get called
print(e);
}
return Future.value();
}
2
Answers
You can also check if image file exists by simply reading it as a file and check its existence using image.isNotEmpty.
Here is way you can limiting your pickup type.
For more refer library document.-> https://pub.dev/packages/image_picker