Showing
2 changed files
with
25 additions
and
3 deletions
| @@ -16,6 +16,10 @@ import 'package:mobile_scanner/src/objects/start_options.dart'; | @@ -16,6 +16,10 @@ import 'package:mobile_scanner/src/objects/start_options.dart'; | ||
| 16 | 16 | ||
| 17 | /// An implementation of [MobileScannerPlatform] that uses method channels. | 17 | /// An implementation of [MobileScannerPlatform] that uses method channels. |
| 18 | class MethodChannelMobileScanner extends MobileScannerPlatform { | 18 | class MethodChannelMobileScanner extends MobileScannerPlatform { |
| 19 | + /// The name of the error event that is sent when a barcode scan error occurs. | ||
| 20 | + @visibleForTesting | ||
| 21 | + static const String kBarcodeErrorEventName = 'MOBILE_SCANNER_BARCODE_ERROR'; | ||
| 22 | + | ||
| 19 | /// The method channel used to interact with the native platform. | 23 | /// The method channel used to interact with the native platform. |
| 20 | @visibleForTesting | 24 | @visibleForTesting |
| 21 | final methodChannel = const MethodChannel( | 25 | final methodChannel = const MethodChannel( |
| @@ -40,11 +44,22 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | @@ -40,11 +44,22 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | ||
| 40 | int? _textureId; | 44 | int? _textureId; |
| 41 | 45 | ||
| 42 | /// 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. | ||
| 43 | BarcodeCapture? _parseBarcode(Map<Object?, Object?>? event) { | 50 | BarcodeCapture? _parseBarcode(Map<Object?, Object?>? event) { |
| 44 | if (event == null) { | 51 | if (event == null) { |
| 45 | return null; | 52 | return null; |
| 46 | } | 53 | } |
| 47 | 54 | ||
| 55 | + if (event | ||
| 56 | + case { | ||
| 57 | + 'name': kBarcodeErrorEventName, | ||
| 58 | + 'data': final String? errorDescription | ||
| 59 | + }) { | ||
| 60 | + throw MobileScannerBarcodeException(errorDescription); | ||
| 61 | + } | ||
| 62 | + | ||
| 48 | final Object? data = event['data']; | 63 | final Object? data = event['data']; |
| 49 | 64 | ||
| 50 | if (data == null || data is! List<Object?>) { | 65 | if (data == null || data is! List<Object?>) { |
| @@ -121,9 +136,13 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | @@ -121,9 +136,13 @@ class MethodChannelMobileScanner extends MobileScannerPlatform { | ||
| 121 | 136 | ||
| 122 | @override | 137 | @override |
| 123 | Stream<BarcodeCapture?> get barcodesStream { | 138 | Stream<BarcodeCapture?> get barcodesStream { |
| 124 | - return eventsStream | ||
| 125 | - .where((event) => event['name'] == 'barcode') | ||
| 126 | - .map((event) => _parseBarcode(event)); | 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)); | ||
| 127 | } | 146 | } |
| 128 | 147 | ||
| 129 | @override | 148 | @override |
| @@ -102,6 +102,9 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { | @@ -102,6 +102,9 @@ class MobileScannerController extends ValueNotifier<MobileScannerState> { | ||
| 102 | StreamController.broadcast(); | 102 | StreamController.broadcast(); |
| 103 | 103 | ||
| 104 | /// Get the stream of scanned barcodes. | 104 | /// Get the stream of scanned barcodes. |
| 105 | + /// | ||
| 106 | + /// If an error occurred during the detection of a barcode, | ||
| 107 | + /// an [MobileScannerBarcodeException] error is emitted to the stream. | ||
| 105 | Stream<BarcodeCapture> get barcodes => _barcodesController.stream; | 108 | Stream<BarcodeCapture> get barcodes => _barcodesController.stream; |
| 106 | 109 | ||
| 107 | StreamSubscription<BarcodeCapture?>? _barcodesSubscription; | 110 | StreamSubscription<BarcodeCapture?>? _barcodesSubscription; |
-
Please register or login to post a comment