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

Commit a27fa25

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

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

packages/camera/camera/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@ 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 error codes that can be thrown:
88+
89+
- CameraAccessDenied
90+
Thrown when user denied the camera access permission.
91+
92+
- CameraAccessDeniedWithoutPrompt
93+
iOS only for now.
94+
Thrown when user has previously denied the permission.
95+
iOS does not allow prompting alert dialog a second time. Users will have to go to Settings > Privacy in order to enable camera access.
96+
97+
- CameraAccessRestricted
98+
iOS only for now.
99+
Thrown when camera access is restricted and users cannot grant permission (parental control).
100+
101+
- cameraPermission
102+
Android and Web only.
103+
A legacy error code for all kinds of camera permission errors.
104+
83105
### Example
84106

85107
Here is a small example flutter app displaying a full screen camera preview.
@@ -119,6 +141,15 @@ class _CameraAppState extends State<CameraApp> {
119141
return;
120142
}
121143
setState(() {});
144+
}).catchError((e) {
145+
switch (e.code) {
146+
case 'CameraAccessDenied':
147+
print('User denied camera access.');
148+
break;
149+
default:
150+
print('Handle other errors.');
151+
break;
152+
}
122153
});
123154
}
124155

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)