Navaron Bracke

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

import 'dart:async';
import 'dart:js';
import 'dart:js_interop';
import 'dart:ui';
import 'package:js/js.dart';
import 'package:mobile_scanner/src/enums/mobile_scanner_error_code.dart';
import 'package:mobile_scanner/src/enums/torch_state.dart';
import 'package:mobile_scanner/src/mobile_scanner_exception.dart';
... ...
... ... @@ -11,32 +11,31 @@ import 'package:mobile_scanner/src/web/zxing/result_point.dart';
///
/// See also: https://github.com/zxing-js/library/blob/master/src/core/Result.ts
@JS()
@anonymous
@staticInterop
abstract class Result {}
extension ResultExt on Result {
/// Get the barcode format.
///
/// See also https://github.com/zxing-js/library/blob/master/src/core/BarcodeFormat.ts
external JSFunction getBarcodeFormat;
@JS('barcodeFormat')
external JSNumber? get _barcodeFormat;
/// Get the raw bytes of the result.
external JSFunction getRawBytes;
@JS('text')
external JSString? get _text;
/// Get the points of the result.
external JSFunction getResultPoints;
@JS('rawBytes')
external JSUint8Array? get _rawBytes;
/// Get the text of the result.
external JSFunction getText;
@JS('resultPoints')
external JSArray? get _resultPoints;
/// Get the timestamp of the result.
external JSFunction getTimestamp;
@JS('timestamp')
external JSNumber? get _timestamp;
/// Get the barcode format of the result.
///
/// See also https://github.com/zxing-js/library/blob/master/src/core/BarcodeFormat.ts
BarcodeFormat get barcodeFormat {
final JSNumber? format = getBarcodeFormat.callAsFunction() as JSNumber?;
switch (format?.toDartInt) {
switch (_barcodeFormat?.toDartInt) {
case 0:
return BarcodeFormat.aztec;
case 1:
... ... @@ -82,36 +81,25 @@ extension ResultExt on Result {
/// Get the corner points of the result.
List<Offset> get resultPoints {
final JSArray? resultPoints = getResultPoints.callAsFunction() as JSArray?;
final JSArray? points = _resultPoints;
if (resultPoints == null) {
if (points == null) {
return [];
}
return resultPoints.toDart.cast<ResultPoint>().map((point) {
return points.toDart.cast<ResultPoint>().map((point) {
return Offset(point.x, point.y);
}).toList();
}
/// Get the raw bytes of the result.
Uint8List? get rawBytes {
final JSUint8Array? rawBytes =
getRawBytes.callAsFunction() as JSUint8Array?;
return rawBytes?.toDart;
}
Uint8List? get rawBytes => _rawBytes?.toDart;
/// Get the text of the result.
String? get text {
return getText.callAsFunction() as String?;
}
String? get text => _text?.toDart;
/// Get the timestamp of the result.
int? get timestamp {
final JSNumber? timestamp = getTimestamp.callAsFunction() as JSNumber?;
return timestamp?.toDartInt;
}
int? get timestamp => _timestamp?.toDartInt;
/// Convert this result to a [Barcode].
Barcode get toBarcode {
... ...
... ... @@ -4,25 +4,20 @@ import 'dart:js_interop';
///
/// See also: https://github.com/zxing-js/library/blob/master/src/core/ResultPoint.ts
@JS()
@anonymous
@staticInterop
abstract class ResultPoint {}
extension ResultPointExt on ResultPoint {
external JSFunction getX;
@JS('x')
external JSNumber get _x;
external JSFunction getY;
@JS('y')
external JSNumber get _y;
/// The x coordinate of the point.
double get x {
final JSNumber? x = getX.callAsFunction() as JSNumber?;
return x?.toDartDouble ?? 0;
}
double get x => _x.toDartDouble;
/// The y coordinate of the point.
double get y {
final JSNumber? y = getY.callAsFunction() as JSNumber?;
return y?.toDartDouble ?? 0;
}
double get y => _y.toDartDouble;
}
... ...
import 'dart:async';
import 'dart:js';
import 'dart:js_interop';
import 'dart:ui';
import 'package:js/js.dart';
import 'package:mobile_scanner/src/enums/barcode_format.dart';
import 'package:mobile_scanner/src/enums/camera_facing.dart';
import 'package:mobile_scanner/src/enums/torch_state.dart';
... ... @@ -161,13 +161,11 @@ final class ZXingBarcodeReader extends BarcodeReader {
_reader?.decodeContinuously.callAsFunction(
null,
_reader?.videoElement,
allowInterop((result, error) {
allowInterop((Result? result, JSAny? error) {
if (!controller.isClosed && result != null) {
final barcode = (result as Result).toBarcode;
controller.add(
BarcodeCapture(
barcodes: [barcode],
barcodes: [result.toBarcode],
),
);
}
... ...
... ... @@ -24,7 +24,6 @@ dependencies:
sdk: flutter
flutter_web_plugins:
sdk: flutter
js: ^0.6.3
plugin_platform_interface: ^2.0.2
web: ^0.3.0
... ... @@ -45,4 +44,4 @@ flutter:
pluginClass: MobileScannerPlugin
web:
pluginClass: MobileScannerWeb
fileName: web/mobile_scanner_web.dart
fileName: src/web/mobile_scanner_web.dart
... ...