I'm trying to understand why my code doesn't work. I'm using the Flutter camera plugin, and want to handle cases when camera permissions are denied. In such cases CameraController.initialize
should throw an exception.
I was trying this out:
CameraController _controller;
//...
await _controller.initialize().catchError((err) {
print(err);
// other stuff
});
If rejecting camera access while running the app, the exception is thrown as expected, but catchError
doesn't catch it, and the app crashes.
So I tried this:
Future<void> doStuff() async {
await Future.delayed(Duration(milliseconds: 1000)); //also tried with Duration.zero
throw Exception("error");
}
//...
await doStuff().catchError((err) {
print(err);
// other stuff
})
In this case, catchError
catches the exception. Why is this happening? What's the difference between _controller.initialize()
and doStuff()
?
I can overcome this by using try / catch instead of catchError
, but I'm interested to find out what's going on in my code.