Navaron Bracke

fix compilation error on web; don't depend on package:js anymore for dart2wasm

1 import 'dart:async'; 1 import 'dart:async';
  2 +import 'dart:js';
2 import 'dart:js_interop'; 3 import 'dart:js_interop';
3 import 'dart:ui'; 4 import 'dart:ui';
4 5
5 -import 'package:js/js.dart';  
6 import 'package:mobile_scanner/src/enums/mobile_scanner_error_code.dart'; 6 import 'package:mobile_scanner/src/enums/mobile_scanner_error_code.dart';
7 import 'package:mobile_scanner/src/enums/torch_state.dart'; 7 import 'package:mobile_scanner/src/enums/torch_state.dart';
8 import 'package:mobile_scanner/src/mobile_scanner_exception.dart'; 8 import 'package:mobile_scanner/src/mobile_scanner_exception.dart';
@@ -11,32 +11,31 @@ import 'package:mobile_scanner/src/web/zxing/result_point.dart'; @@ -11,32 +11,31 @@ import 'package:mobile_scanner/src/web/zxing/result_point.dart';
11 /// 11 ///
12 /// See also: https://github.com/zxing-js/library/blob/master/src/core/Result.ts 12 /// See also: https://github.com/zxing-js/library/blob/master/src/core/Result.ts
13 @JS() 13 @JS()
  14 +@anonymous
14 @staticInterop 15 @staticInterop
15 abstract class Result {} 16 abstract class Result {}
16 17
17 extension ResultExt on Result { 18 extension ResultExt on Result {
18 - /// Get the barcode format.  
19 - ///  
20 - /// See also https://github.com/zxing-js/library/blob/master/src/core/BarcodeFormat.ts  
21 - external JSFunction getBarcodeFormat; 19 + @JS('barcodeFormat')
  20 + external JSNumber? get _barcodeFormat;
22 21
23 - /// Get the raw bytes of the result.  
24 - external JSFunction getRawBytes; 22 + @JS('text')
  23 + external JSString? get _text;
25 24
26 - /// Get the points of the result.  
27 - external JSFunction getResultPoints; 25 + @JS('rawBytes')
  26 + external JSUint8Array? get _rawBytes;
28 27
29 - /// Get the text of the result.  
30 - external JSFunction getText; 28 + @JS('resultPoints')
  29 + external JSArray? get _resultPoints;
31 30
32 - /// Get the timestamp of the result.  
33 - external JSFunction getTimestamp; 31 + @JS('timestamp')
  32 + external JSNumber? get _timestamp;
34 33
35 /// Get the barcode format of the result. 34 /// Get the barcode format of the result.
  35 + ///
  36 + /// See also https://github.com/zxing-js/library/blob/master/src/core/BarcodeFormat.ts
36 BarcodeFormat get barcodeFormat { 37 BarcodeFormat get barcodeFormat {
37 - final JSNumber? format = getBarcodeFormat.callAsFunction() as JSNumber?;  
38 -  
39 - switch (format?.toDartInt) { 38 + switch (_barcodeFormat?.toDartInt) {
40 case 0: 39 case 0:
41 return BarcodeFormat.aztec; 40 return BarcodeFormat.aztec;
42 case 1: 41 case 1:
@@ -82,36 +81,25 @@ extension ResultExt on Result { @@ -82,36 +81,25 @@ extension ResultExt on Result {
82 81
83 /// Get the corner points of the result. 82 /// Get the corner points of the result.
84 List<Offset> get resultPoints { 83 List<Offset> get resultPoints {
85 - final JSArray? resultPoints = getResultPoints.callAsFunction() as JSArray?; 84 + final JSArray? points = _resultPoints;
86 85
87 - if (resultPoints == null) { 86 + if (points == null) {
88 return []; 87 return [];
89 } 88 }
90 89
91 - return resultPoints.toDart.cast<ResultPoint>().map((point) { 90 + return points.toDart.cast<ResultPoint>().map((point) {
92 return Offset(point.x, point.y); 91 return Offset(point.x, point.y);
93 }).toList(); 92 }).toList();
94 } 93 }
95 94
96 /// Get the raw bytes of the result. 95 /// Get the raw bytes of the result.
97 - Uint8List? get rawBytes {  
98 - final JSUint8Array? rawBytes =  
99 - getRawBytes.callAsFunction() as JSUint8Array?;  
100 -  
101 - return rawBytes?.toDart;  
102 - } 96 + Uint8List? get rawBytes => _rawBytes?.toDart;
103 97
104 /// Get the text of the result. 98 /// Get the text of the result.
105 - String? get text {  
106 - return getText.callAsFunction() as String?;  
107 - } 99 + String? get text => _text?.toDart;
108 100
109 /// Get the timestamp of the result. 101 /// Get the timestamp of the result.
110 - int? get timestamp {  
111 - final JSNumber? timestamp = getTimestamp.callAsFunction() as JSNumber?;  
112 -  
113 - return timestamp?.toDartInt;  
114 - } 102 + int? get timestamp => _timestamp?.toDartInt;
115 103
116 /// Convert this result to a [Barcode]. 104 /// Convert this result to a [Barcode].
117 Barcode get toBarcode { 105 Barcode get toBarcode {
@@ -4,25 +4,20 @@ import 'dart:js_interop'; @@ -4,25 +4,20 @@ import 'dart:js_interop';
4 /// 4 ///
5 /// See also: https://github.com/zxing-js/library/blob/master/src/core/ResultPoint.ts 5 /// See also: https://github.com/zxing-js/library/blob/master/src/core/ResultPoint.ts
6 @JS() 6 @JS()
  7 +@anonymous
7 @staticInterop 8 @staticInterop
8 abstract class ResultPoint {} 9 abstract class ResultPoint {}
9 10
10 extension ResultPointExt on ResultPoint { 11 extension ResultPointExt on ResultPoint {
11 - external JSFunction getX; 12 + @JS('x')
  13 + external JSNumber get _x;
12 14
13 - external JSFunction getY; 15 + @JS('y')
  16 + external JSNumber get _y;
14 17
15 /// The x coordinate of the point. 18 /// The x coordinate of the point.
16 - double get x {  
17 - final JSNumber? x = getX.callAsFunction() as JSNumber?;  
18 -  
19 - return x?.toDartDouble ?? 0;  
20 - } 19 + double get x => _x.toDartDouble;
21 20
22 /// The y coordinate of the point. 21 /// The y coordinate of the point.
23 - double get y {  
24 - final JSNumber? y = getY.callAsFunction() as JSNumber?;  
25 -  
26 - return y?.toDartDouble ?? 0;  
27 - } 22 + double get y => _y.toDartDouble;
28 } 23 }
1 import 'dart:async'; 1 import 'dart:async';
  2 +import 'dart:js';
2 import 'dart:js_interop'; 3 import 'dart:js_interop';
3 import 'dart:ui'; 4 import 'dart:ui';
4 5
5 -import 'package:js/js.dart';  
6 import 'package:mobile_scanner/src/enums/barcode_format.dart'; 6 import 'package:mobile_scanner/src/enums/barcode_format.dart';
7 import 'package:mobile_scanner/src/enums/camera_facing.dart'; 7 import 'package:mobile_scanner/src/enums/camera_facing.dart';
8 import 'package:mobile_scanner/src/enums/torch_state.dart'; 8 import 'package:mobile_scanner/src/enums/torch_state.dart';
@@ -161,13 +161,11 @@ final class ZXingBarcodeReader extends BarcodeReader { @@ -161,13 +161,11 @@ final class ZXingBarcodeReader extends BarcodeReader {
161 _reader?.decodeContinuously.callAsFunction( 161 _reader?.decodeContinuously.callAsFunction(
162 null, 162 null,
163 _reader?.videoElement, 163 _reader?.videoElement,
164 - allowInterop((result, error) { 164 + allowInterop((Result? result, JSAny? error) {
165 if (!controller.isClosed && result != null) { 165 if (!controller.isClosed && result != null) {
166 - final barcode = (result as Result).toBarcode;  
167 -  
168 controller.add( 166 controller.add(
169 BarcodeCapture( 167 BarcodeCapture(
170 - barcodes: [barcode], 168 + barcodes: [result.toBarcode],
171 ), 169 ),
172 ); 170 );
173 } 171 }
@@ -24,7 +24,6 @@ dependencies: @@ -24,7 +24,6 @@ dependencies:
24 sdk: flutter 24 sdk: flutter
25 flutter_web_plugins: 25 flutter_web_plugins:
26 sdk: flutter 26 sdk: flutter
27 - js: ^0.6.3  
28 plugin_platform_interface: ^2.0.2 27 plugin_platform_interface: ^2.0.2
29 web: ^0.3.0 28 web: ^0.3.0
30 29
@@ -45,4 +44,4 @@ flutter: @@ -45,4 +44,4 @@ flutter:
45 pluginClass: MobileScannerPlugin 44 pluginClass: MobileScannerPlugin
46 web: 45 web:
47 pluginClass: MobileScannerWeb 46 pluginClass: MobileScannerWeb
48 - fileName: web/mobile_scanner_web.dart 47 + fileName: src/web/mobile_scanner_web.dart