startVerification method

Future<VerificationResult<ScanResult>> startVerification({
  1. String mode = 'match',
  2. void onDocumentScanned(
    1. ScanResult
    )?,
  3. void onLivenessCaptured(
    1. Uint8List
    )?,
  4. void onFaceMatchComplete(
    1. bool matched,
    2. double? similarity
    )?,
})

Start the complete verification process

This orchestrates the entire verification flow including:

  1. Document scanning
  2. Face liveness detection
  3. Face matching

Parameters:

  • mode: Verification mode ('match' or 'liveness')
  • onDocumentScanned: Callback when document scan completes
  • onLivenessCaptured: Callback when liveness photo is captured
  • onFaceMatchComplete: Callback when face matching completes

Returns: A VerificationResult containing either the scan result or an error

Throws:

Example:

final result = await GetVerified.instance.startVerification(
  mode: 'match',
  onDocumentScanned: (scanResult) {
    print('Document scanned: ${scanResult.name}');
  },
  onFaceMatchComplete: (matched, similarity) {
    print('Face match: $matched (${similarity}%)');
  },
);

result.fold(
  onSuccess: (data) => print('Success: ${data.name}'),
  onFailure: (error) => print('Failed: ${error.message}'),
);

Implementation

Future<VerificationResult<ScanResult>> startVerification({
  String mode = 'match',
  void Function(ScanResult)? onDocumentScanned,
  void Function(Uint8List)? onLivenessCaptured,
  void Function(bool matched, double? similarity)? onFaceMatchComplete,
}) async {
  if (!_isInitialized) {
    return VerificationResult.failure(
      const SDKInitializationException(
        'SDK not initialized. Call initialize() first.',
        code: ErrorCodes.initializationFailed,
      ),
    );
  }

  try {
    final solution = GetVerifiedSolution();
    ScanResult? finalResult;
    GetVerifiedException? capturedError;

    await solution.startProcess(
      faceApiUrl: _config!.faceApiUrl!,
      mode: mode,
      documentReaderConfig: _config!.documentReader,
      faceSDKConfig: _config!.faceSDK,
      onDocumentFetched: (scanResult) {
        onDocumentScanned?.call(scanResult);
      },
      onFaceMismatch: () {
        capturedError = const FaceVerificationException(
          'Face verification failed - faces do not match',
          code: ErrorCodes.faceMatchFailed,
        );
      },
      onVerificationSuccess: (scanResult) {
        finalResult = scanResult;
        onFaceMatchComplete?.call(true, null);
      },
      onError: (error) {
        capturedError = error;
      },
    );

    // Wait a bit for async callbacks to complete
    await Future.delayed(const Duration(milliseconds: 500));

    if (capturedError != null) {
      return VerificationResult.failure(capturedError!);
    }

    if (finalResult != null) {
      return VerificationResult.success(finalResult!);
    }

    return VerificationResult.failure(
      const DocumentScanException(
        'Verification process did not complete',
        code: ErrorCodes.scanFailed,
      ),
    );
  } on GetVerifiedException catch (e) {
    return VerificationResult.failure(e);
  } catch (e, stackTrace) {
    return VerificationResult.failure(
      GetVerifiedException(
        'Unexpected error during verification: $e',
        originalError: e,
        stackTrace: stackTrace,
      ),
    );
  }
}