startPreview method

  1. @override
Future<int> startPreview()
override

Starts the native preview stream and returns a texture id.

The camera must be opened with open first. Pass the returned id to buildPreview to display the video feed. Frames are drawn entirely at the native layer; no pixel data crosses the platform channel.

Implementation

@override
Future<int> startPreview() async {
  final element = _videoElement;
  if (element == null) {
    throw PlatformException(
        code: 'CAMERA_ERROR', message: 'Camera is not open.');
  }

  await _play();

  final id = _nextTextureId++;
  final viewType = 'flutter_lite_camera_$id';
  _viewTypes[id] = viewType;

  // Wrap the video in a sized div so the platform view fills its slot with
  // object-fit behavior from the video element itself.
  element.style
    ..width = '100%'
    ..height = '100%'
    ..objectFit = 'cover';
  final wrapper = dom.HTMLDivElement()
    ..style.width = '100%'
    ..style.height = '100%'
    ..style.overflow = 'hidden'
    ..append(element);
  ui_web.platformViewRegistry.registerViewFactory(viewType, (_) => wrapper);
  return id;
}