In my application I want to take image with camera, and I'm using image_picker
library for that.
Here is my code:
import 'dart:io';
import 'package:image_picker/image_picker.dart';
Future<File> getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.camera);
return image;
}
It works fine, but if camera is not available for some reason, then it will crash the application. So, I think we can avoid that by using it inside try/catch
(please correct me if I'm wrong).
I added try/catch
, and here is the updated code:
try {
var image = await ImagePicker.pickImage(source: ImageSource.camera);
} catch (e) {
print(e);
}
return image;
My IDE throws error, variable image
is not defined.
Questions:
- How do I use
try/catch
properly in this case? - Does
try/catch
is the approach for these kind of issues? - Is there any other error/exception, I should care about?