scan static method

Future<ScanResult> scan({
  1. ScanOptions options = const ScanOptions(),
})

Starts the camera for scanning the barcode, shows a preview window and returns the barcode if one was scanned. Can throw an exception. See also cameraAccessDenied

Implementation

static Future<ScanResult> scan({
  ScanOptions options = const ScanOptions(),
}) async {
  if (Platform.isIOS) {
    return _doScan(options);
  }

  final events = _eventChannel.receiveBroadcastStream();
  final completer = Completer<ScanResult>();

  late StreamSubscription subscription;
  subscription = events.listen((dynamic event) async {
    if (event is String) {
      if (event == cameraAccessGranted) {
        subscription.cancel();
        completer.complete(await _doScan(options));
      } else if (event == cameraAccessDenied) {
        subscription.cancel();
        completer.completeError(PlatformException(code: event));
      }
    }
  });

  final permissionsRequested =
      (await _channel.invokeMethod<bool>('requestCameraPermission'))!;

  if (permissionsRequested) {
    return completer.future;
  } else {
    subscription.cancel();
    return _doScan(options);
  }
}