handleCompletion method

void handleCompletion(
  1. DocReaderAction action,
  2. Results? result,
  3. DocReaderException? error
)

Implementation

void handleCompletion(
  DocReaderAction action,
  Results? result,
  DocReaderException? error,
) async {
  debugPrint("action: $action");
  debugPrint("result: ${result?.transactionInfo?.transactionId}");
  debugPrint("CheckResult: ${result?.status.portrait.value}");
  debugPrint("error: ${error?.message}");

  if (error != null) {
    if (_onError != null) {
      _onError!(
        DocumentScanException(
          error.message,
          code: error.code.toString(),
          originalError: error,
        ),
      );
    }
    return;
  }

  if (action == DocReaderAction.CANCEL || action == DocReaderAction.ERROR) {
    if (_onError != null) {
      _onError!(
        const UserCancelledException(
          "Scanning cancelled or failed",
          code: core_error.ErrorCodes.scanCancelled,
        ),
      );
    }
    return;
  }

  if (action == DocReaderAction.COMPLETE && result != null) {
    // Convert and emit scan results
    try {
      final scanResult = await _convertToScanResult(result);

      final (face, faceLivenessTransactionId) = await faceSdkSteps
          .startLiveness();
      if (face != null) {
        String base64String = base64Encode(face);
        debugPrint("base64String of live: $base64String");
      }

      if (_onDocumentFetched != null) {
        _onDocumentFetched!(scanResult);
      }

      var doc1 = await result.graphicFieldImageByType(
        GraphicFieldType.PORTRAIT,
      );

      debugPrint("face image: $face");
      // Check if doc1 (Portrait from document) is available for matching
      if (doc1 == null && _mode == "match") {
        // If we are in match mode but no portrait found in document
        debugPrint("No portrait found in document for matching");
      }

      var isMatched = await faceSdkSteps.matchFaces(doc1, face);

      debugPrint("isMatched: $isMatched");
      if (isMatched) {
        final transactionId = await docReaderSdkSteps.finalise();
        debugPrint("Transaction ID: $transactionId");
        // Emit completion event
        if (_onVerificationSuccess != null) {
          _onVerificationSuccess!(
            scanResult.copyWith(
              transactionId: transactionId,
              selfieImage: face,
              faceLivenessTransactionId: faceLivenessTransactionId,
            ),
          );
        }
      } else {
        if (_onFaceMismatch != null) {
          _onFaceMismatch!();
        }
      }
    } catch (e, stackTrace) {
      if (_onError != null) {
        _onError!(
          FaceVerificationException(
            "Error during verification processing: $e",
            code: core_error.ErrorCodes.faceMatchFailed,
            originalError: e,
            stackTrace: stackTrace,
          ),
        );
      }
    }
  }
}