when<T> method

T? when<T>({
  1. T nv21(
    1. Nv21Image image
    )?,
  2. T bgra8888(
    1. Bgra8888Image image
    )?,
  3. T jpeg(
    1. JpegImage image
    )?,
  4. T yuv420(
    1. Yuv420Image image
    )?,
})

Helper function to decide what to do depending on the AnalysisImage format with type safe checks.

Implementation

T? when<T>({
  T Function(Nv21Image image)? nv21,
  T Function(Bgra8888Image image)? bgra8888,
  T Function(JpegImage image)? jpeg,
  T Function(Yuv420Image image)? yuv420,
}) {
  if (this is Nv21Image) {
    return nv21?.call(this as Nv21Image);
  } else if (this is Bgra8888Image) {
    return bgra8888?.call(this as Bgra8888Image);
  } else if (this is JpegImage) {
    return jpeg?.call(this as JpegImage);
  } else if (this is Yuv420Image) {
    return yuv420?.call(this as Yuv420Image);
  } else {
    throw "Unsupported AnalysisImage format: $format";
  }
}