Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit b460b80

Browse files
committed
[camera]add error handling in example, and update readme
1 parent d20b45c commit b460b80

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

packages/camera/camera/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ void didChangeAppLifecycleState(AppLifecycleState state) {
8080
}
8181
```
8282

83+
### Handling camera access permissions
84+
85+
Permission errors may be thrown when initializing the camera controller, and you are expected to handle them properly.
86+
87+
Here is a list of all permission errors that can be thrown:
88+
89+
- CameraAccessDenied
90+
Thrown when user denied the permission.
91+
92+
- CameraAccessDeniedWithoutPrompt
93+
Thrown when user previously denied the permission.
94+
iOS only for now. iOS does not allow prompting alert dialog a second time. Users will have to go to Settings > Privacy in order to enable camera access.
95+
96+
- CameraAccessRestricted
97+
Thrown when camera access is restricted and users cannot grant permission (parental control). iOS only for now.
98+
99+
- cameraPermission
100+
A legacy error code for all kinds of camera permission errors. Currently used by Android and Web.
101+
83102
### Example
84103

85104
Here is a small example flutter app displaying a full screen camera preview.
@@ -119,6 +138,15 @@ class _CameraAppState extends State<CameraApp> {
119138
return;
120139
}
121140
setState(() {});
141+
}).catchError((e) {
142+
switch (e.code) {
143+
case 'CameraAccessDenied':
144+
print('user denied camera access');
145+
break;
146+
default:
147+
print('other errors');
148+
break;
149+
}
122150
});
123151
}
124152

packages/camera/camera/example/lib/main.dart

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,26 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
685685
.then((double value) => _minAvailableZoom = value),
686686
]);
687687
} on CameraException catch (e) {
688-
_showCameraException(e);
688+
switch (e.code) {
689+
case 'CameraAccessDenied':
690+
showInSnackBar('You have denied camera access.');
691+
break;
692+
case 'CameraAccessDeniedWithoutPrompt':
693+
// iOS only
694+
showInSnackBar('Please go to Settings app to enable camera access.');
695+
break;
696+
case 'CameraAccessRestricted':
697+
// iOS only
698+
showInSnackBar('Camera access is restricted.');
699+
break;
700+
case 'cameraPermission':
701+
// Android & web only
702+
showInSnackBar('Legacy error code for all kinds of permission errors');
703+
break;
704+
default:
705+
_showCameraException(e);
706+
break;
707+
}
689708
}
690709

691710
if (mounted) {

0 commit comments

Comments
 (0)