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 {
  assert(options != null);
  if (Platform.isIOS) {
    return _doScan(options);
  }

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

  StreamSubscription? subscription;
  subscription = events.listen((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));
      }
    }
  });

  var permissionsRequested =
      await _channel!.invokeMethod('requestCameraPermission');

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