handle barcode error events in the method channel implementation
Showing
1 changed file
with
19 additions
and
18 deletions
| @@ -44,22 +44,11 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | @@ -44,22 +44,11 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | ||
| 44 | int? _textureId; | 44 | int? _textureId; |
| 45 | 45 | ||
| 46 | /// Parse a [BarcodeCapture] from the given [event]. | 46 | /// Parse a [BarcodeCapture] from the given [event]. |
| 47 | - /// | ||
| 48 | - /// If the event name is [kBarcodeErrorEventName], | ||
| 49 | - /// a [MobileScannerBarcodeException] is thrown. | ||
| 50 | BarcodeCapture? _parseBarcode(Map<Object?, Object?>? event) { | 47 | BarcodeCapture? _parseBarcode(Map<Object?, Object?>? event) { |
| 51 | if (event == null) { | 48 | if (event == null) { |
| 52 | return null; | 49 | return null; |
| 53 | } | 50 | } |
| 54 | 51 | ||
| 55 | - if (event | ||
| 56 | - case { | ||
| 57 | - 'name': kBarcodeErrorEventName, | ||
| 58 | - 'data': final String? errorDescription | ||
| 59 | - }) { | ||
| 60 | - throw MobileScannerBarcodeException(errorDescription); | ||
| 61 | - } | ||
| 62 | - | ||
| 63 | final Object? data = event['data']; | 52 | final Object? data = event['data']; |
| 64 | 53 | ||
| 65 | if (data == null || data is! List<Object?>) { | 54 | if (data == null || data is! List<Object?>) { |
| @@ -94,6 +83,19 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | @@ -94,6 +83,19 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | ||
| 94 | ); | 83 | ); |
| 95 | } | 84 | } |
| 96 | 85 | ||
| 86 | + /// Parse a [MobileScannerBarcodeException] from the given [error] and [stackTrace], and throw it. | ||
| 87 | + /// | ||
| 88 | + /// If the error is not a [PlatformException], | ||
| 89 | + /// with [kBarcodeErrorEventName] as [PlatformException.code], the error is rethrown as-is. | ||
| 90 | + Never _parseBarcodeError(Object error, StackTrace stackTrace) { | ||
| 91 | + if (error case PlatformException(:final String code, :final String? message) | ||
| 92 | + when code == kBarcodeErrorEventName) { | ||
| 93 | + throw MobileScannerBarcodeException(message); | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + Error.throwWithStackTrace(error, stackTrace); | ||
| 97 | + } | ||
| 98 | + | ||
| 97 | /// Request permission to access the camera. | 99 | /// Request permission to access the camera. |
| 98 | /// | 100 | /// |
| 99 | /// Throws a [MobileScannerException] if the permission is not granted. | 101 | /// Throws a [MobileScannerException] if the permission is not granted. |
| @@ -136,13 +138,12 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | @@ -136,13 +138,12 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | ||
| 136 | 138 | ||
| 137 | @override | 139 | @override |
| 138 | Stream<BarcodeCapture?> get barcodesStream { | 140 | Stream<BarcodeCapture?> get barcodesStream { |
| 139 | - // Handle both incoming barcode events and barcode error events. | ||
| 140 | - return eventsStream.where( | ||
| 141 | - (event) { | ||
| 142 | - return event['name'] == 'barcode' || | ||
| 143 | - event['name'] == kBarcodeErrorEventName; | ||
| 144 | - }, | ||
| 145 | - ).map((event) => _parseBarcode(event)); | 141 | + // Handle incoming barcode events. |
| 142 | + // The error events are transformed to `MobileScannerBarcodeException` where possible. | ||
| 143 | + return eventsStream | ||
| 144 | + .where((e) => e['name'] == 'barcode') | ||
| 145 | + .map((event) => _parseBarcode(event)) | ||
| 146 | + .handleError(_parseBarcodeError); | ||
| 146 | } | 147 | } |
| 147 | 148 | ||
| 148 | @override | 149 | @override |
-
Please register or login to post a comment