Navaron Bracke

handle errors from controller.start() in example app

... ... @@ -26,6 +26,31 @@ class _BarcodeListScannerWithControllerState
bool isStarted = true;
void _startOrStop() {
if (isStarted) {
controller.stop();
} else {
controller.start().catchError((error) {
final exception = error as MobileScannerException;
switch (exception.errorCode) {
case MobileScannerErrorCode.controllerUninitialized:
break; // This error code is not used by `start()`.
case MobileScannerErrorCode.genericError:
debugPrint('Scanner failed to start');
break;
case MobileScannerErrorCode.permissionDenied:
debugPrint('Camera permission denied');
break;
}
});
}
setState(() {
isStarted = !isStarted;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
... ... @@ -93,10 +118,7 @@ class _BarcodeListScannerWithControllerState
? const Icon(Icons.stop)
: const Icon(Icons.play_arrow),
iconSize: 32.0,
onPressed: () => setState(() {
isStarted ? controller.stop() : controller.start();
isStarted = !isStarted;
}),
onPressed: _startOrStop,
),
Center(
child: SizedBox(
... ...
... ... @@ -26,6 +26,31 @@ class _BarcodeScannerWithControllerState
bool isStarted = true;
void _startOrStop() {
if (isStarted) {
controller.stop();
} else {
controller.start().catchError((error) {
final exception = error as MobileScannerException;
switch (exception.errorCode) {
case MobileScannerErrorCode.controllerUninitialized:
break; // This error code is not used by `start()`.
case MobileScannerErrorCode.genericError:
debugPrint('Scanner failed to start');
break;
case MobileScannerErrorCode.permissionDenied:
debugPrint('Camera permission denied');
break;
}
});
}
setState(() {
isStarted = !isStarted;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
... ... @@ -90,10 +115,7 @@ class _BarcodeScannerWithControllerState
? const Icon(Icons.stop)
: const Icon(Icons.play_arrow),
iconSize: 32.0,
onPressed: () => setState(() {
isStarted ? controller.stop() : controller.start();
isStarted = !isStarted;
}),
onPressed: _startOrStop,
),
Center(
child: SizedBox(
... ...
... ... @@ -28,6 +28,31 @@ class _BarcodeScannerReturningImageState
bool isStarted = true;
void _startOrStop() {
if (isStarted) {
controller.stop();
} else {
controller.start().catchError((error) {
final exception = error as MobileScannerException;
switch (exception.errorCode) {
case MobileScannerErrorCode.controllerUninitialized:
break; // This error code is not used by `start()`.
case MobileScannerErrorCode.genericError:
debugPrint('Scanner failed to start');
break;
case MobileScannerErrorCode.permissionDenied:
debugPrint('Camera permission denied');
break;
}
});
}
setState(() {
isStarted = !isStarted;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
... ... @@ -112,12 +137,7 @@ class _BarcodeScannerReturningImageState
? const Icon(Icons.stop)
: const Icon(Icons.play_arrow),
iconSize: 32.0,
onPressed: () => setState(() {
isStarted
? controller.stop()
: controller.start();
isStarted = !isStarted;
}),
onPressed: _startOrStop,
),
Center(
child: SizedBox(
... ...