Navaron Bracke
Committed by GitHub

Merge pull request #808 from navaronbracke/unhandled_platform_exception

fix: Handle unhandled exception from the state call
... ... @@ -2,6 +2,8 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mobile_scanner/src/enums/mobile_scanner_error_code.dart';
import 'package:mobile_scanner/src/mobile_scanner_controller.dart';
import 'package:mobile_scanner/src/mobile_scanner_exception.dart';
import 'package:mobile_scanner/src/objects/barcode_capture.dart';
... ... @@ -138,11 +140,31 @@ class _MobileScannerState extends State<MobileScanner>
widget.onStart?.call(arguments);
widget.onScannerStarted?.call(arguments);
}).catchError((error) {
if (mounted) {
setState(() {
_startException = error as MobileScannerException;
});
if (!mounted) {
return;
}
if (error is MobileScannerException) {
_startException = error;
} else if (error is PlatformException) {
_startException = MobileScannerException(
errorCode: MobileScannerErrorCode.genericError,
errorDetails: MobileScannerErrorDetails(
code: error.code,
message: error.message,
details: error.details,
),
);
} else {
_startException = MobileScannerException(
errorCode: MobileScannerErrorCode.genericError,
errorDetails: MobileScannerErrorDetails(
details: error,
),
);
}
setState(() {});
});
}
... ...
... ... @@ -185,8 +185,24 @@ class MobileScannerController {
// Check authorization status
if (!kIsWeb) {
final MobileScannerState state = MobileScannerState
final MobileScannerState state;
try {
state = MobileScannerState
.values[await _methodChannel.invokeMethod('state') as int? ?? 0];
} on PlatformException catch (error) {
isStarting = false;
throw MobileScannerException(
errorCode: MobileScannerErrorCode.genericError,
errorDetails: MobileScannerErrorDetails(
code: error.code,
details: error.details as Object?,
message: error.message,
),
);
}
switch (state) {
case MobileScannerState.undetermined:
bool result = false;
... ...