Toggle navigation
Toggle navigation
This project
Loading...
Sign in
flutter_package
/
mobile_scanner
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Navaron Bracke
2024-04-08 13:59:00 +0200
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
3cf8f5535965488a1cdb5b61a09dc9e42c823d6b
3cf8f553
1 parent
3cdac6ee
use a single try catch block for the camera permission
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
34 deletions
lib/src/method_channel/mobile_scanner_method_channel.dart
lib/src/method_channel/mobile_scanner_method_channel.dart
View file @
3cf8f55
...
...
@@ -94,12 +94,29 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
///
/// Throws a [MobileScannerException] if the permission is not granted.
Future
<
void
>
_requestCameraPermission
()
async
{
final
MobileScannerAuthorizationState
authorizationState
;
try
{
authorizationState
=
MobileScannerAuthorizationState
.
fromRawValue
(
final
MobileScannerAuthorizationState
authorizationState
=
MobileScannerAuthorizationState
.
fromRawValue
(
await
methodChannel
.
invokeMethod
<
int
>(
'state'
)
??
0
,
);
switch
(
authorizationState
)
{
// Authorization was already granted, no need to request it again.
case
MobileScannerAuthorizationState
.
authorized
:
return
;
// Android does not have an undetermined authorization state.
// So if the permission was denied, request it again.
case
MobileScannerAuthorizationState
.
denied
:
case
MobileScannerAuthorizationState
.
undetermined
:
final
bool
permissionGranted
=
await
methodChannel
.
invokeMethod
<
bool
>(
'request'
)
??
false
;
if
(!
permissionGranted
)
{
throw
const
MobileScannerException
(
errorCode:
MobileScannerErrorCode
.
permissionDenied
,
);
}
}
}
on
PlatformException
catch
(
error
)
{
// If the permission state is invalid, that is an error.
throw
MobileScannerException
(
...
...
@@ -111,37 +128,6 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
),
);
}
switch
(
authorizationState
)
{
case
MobileScannerAuthorizationState
.
authorized
:
return
;
// Already authorized.
// Android does not have an undetermined authorization state.
// So if the permission was denied, request it again.
case
MobileScannerAuthorizationState
.
denied
:
case
MobileScannerAuthorizationState
.
undetermined
:
try
{
final
bool
granted
=
await
methodChannel
.
invokeMethod
<
bool
>(
'request'
)
??
false
;
if
(
granted
)
{
return
;
// Authorization was granted.
}
throw
const
MobileScannerException
(
errorCode:
MobileScannerErrorCode
.
permissionDenied
,
);
}
on
PlatformException
catch
(
error
)
{
// If the permission state is invalid, that is an error.
throw
MobileScannerException
(
errorCode:
MobileScannerErrorCode
.
genericError
,
errorDetails:
MobileScannerErrorDetails
(
code:
error
.
code
,
details:
error
.
details
as
Object
?,
message:
error
.
message
,
),
);
}
}
}
@override
...
...
Please
register
or
login
to post a comment