@@ -44,22 +44,11 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
44
44
int ? _textureId;
45
45
46
46
/// Parse a [BarcodeCapture] from the given [event] .
47
- ///
48
- /// If the event name is [kBarcodeErrorEventName] ,
49
- /// a [MobileScannerBarcodeException] is thrown.
50
47
BarcodeCapture ? _parseBarcode (Map <Object ?, Object ?>? event) {
51
48
if (event == null ) {
52
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
52
final Object ? data = event['data' ];
64
53
65
54
if (data == null || data is ! List <Object ?>) {
@@ -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
99
/// Request permission to access the camera.
98
100
///
99
101
/// Throws a [MobileScannerException] if the permission is not granted.
@@ -136,13 +138,12 @@ class MethodChannelMobileScanner extends MobileScannerPlatform {
136
138
137
139
@override
138
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
149
@override
0 commit comments