Navaron Bracke

use a single try catch block for the camera permission

@@ -94,42 +94,29 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { @@ -94,42 +94,29 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
94 /// 94 ///
95 /// Throws a [MobileScannerException] if the permission is not granted. 95 /// Throws a [MobileScannerException] if the permission is not granted.
96 Future<void> _requestCameraPermission() async { 96 Future<void> _requestCameraPermission() async {
97 - final MobileScannerAuthorizationState authorizationState;  
98 -  
99 try { 97 try {
100 - authorizationState = MobileScannerAuthorizationState.fromRawValue( 98 + final MobileScannerAuthorizationState authorizationState =
  99 + MobileScannerAuthorizationState.fromRawValue(
101 await methodChannel.invokeMethod<int>('state') ?? 0, 100 await methodChannel.invokeMethod<int>('state') ?? 0,
102 ); 101 );
103 - } on PlatformException catch (error) {  
104 - // If the permission state is invalid, that is an error.  
105 - throw MobileScannerException(  
106 - errorCode: MobileScannerErrorCode.genericError,  
107 - errorDetails: MobileScannerErrorDetails(  
108 - code: error.code,  
109 - details: error.details as Object?,  
110 - message: error.message,  
111 - ),  
112 - );  
113 - }  
114 102
115 switch (authorizationState) { 103 switch (authorizationState) {
  104 + // Authorization was already granted, no need to request it again.
116 case MobileScannerAuthorizationState.authorized: 105 case MobileScannerAuthorizationState.authorized:
117 - return; // Already authorized. 106 + return;
118 // Android does not have an undetermined authorization state. 107 // Android does not have an undetermined authorization state.
119 // So if the permission was denied, request it again. 108 // So if the permission was denied, request it again.
120 case MobileScannerAuthorizationState.denied: 109 case MobileScannerAuthorizationState.denied:
121 case MobileScannerAuthorizationState.undetermined: 110 case MobileScannerAuthorizationState.undetermined:
122 - try {  
123 - final bool granted = 111 + final bool permissionGranted =
124 await methodChannel.invokeMethod<bool>('request') ?? false; 112 await methodChannel.invokeMethod<bool>('request') ?? false;
125 113
126 - if (granted) {  
127 - return; // Authorization was granted.  
128 - }  
129 - 114 + if (!permissionGranted) {
130 throw const MobileScannerException( 115 throw const MobileScannerException(
131 errorCode: MobileScannerErrorCode.permissionDenied, 116 errorCode: MobileScannerErrorCode.permissionDenied,
132 ); 117 );
  118 + }
  119 + }
133 } on PlatformException catch (error) { 120 } on PlatformException catch (error) {
134 // If the permission state is invalid, that is an error. 121 // If the permission state is invalid, that is an error.
135 throw MobileScannerException( 122 throw MobileScannerException(
@@ -142,7 +129,6 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { @@ -142,7 +129,6 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
142 ); 129 );
143 } 130 }
144 } 131 }
145 - }  
146 132
147 @override 133 @override
148 Stream<BarcodeCapture?> get barcodesStream { 134 Stream<BarcodeCapture?> get barcodesStream {