initialize method

Future<void> initialize(
  1. List<BarcodeType> types,
  2. Resolution resolution,
  3. Framerate framerate,
  4. DetectionMode detectionMode,
  5. CameraPosition position,
  6. void onScan(
    1. Barcode
    )?,
)

Informs the platform to initialize the camera.

The camera is disposed and reinitialized when calling this method repeatedly. Events and errors are received via the current state's eventNotifier.

Implementation

Future<void> initialize(
    List<BarcodeType> types,
    Resolution resolution,
    Framerate framerate,
    DetectionMode detectionMode,
    CameraPosition position,
    void Function(Barcode)? onScan) async {
  state.eventNotifier.value = CameraEvent.init;

  try {
    if (state.isInitialized) await _platform.dispose();
    state._previewConfig = await _platform.init(
        types, resolution, framerate, detectionMode, position);

    /// Notify the overlays when a barcode is detected and then call [onDetect].
    _platform.setOnDetectHandler((code) {
      state.eventNotifier.value = CameraEvent.codeFound;
      onScan?.call(code);
    });

    state.eventNotifier.value = CameraEvent.resumed;
  } catch (error, stack) {
    state._error = error;
    state.eventNotifier.value = CameraEvent.error;
    debugPrint(error.toString());
    debugPrintStack(stackTrace: stack);
    return;
  }
}