getCamera function

Future<CameraDescription?> getCamera(
  1. CameraLensDirection dir
)

Get camera description for the given direction, or null if unavailable.

Implementation

Future<CameraDescription?> getCamera(CameraLensDirection dir) async {
  try {
    final cameras = await availableCameras();
    if (cameras.isEmpty) return null;
    if (cameras.length == 1) return cameras[0];
    return cameras.firstWhere(
      (camera) => camera.lensDirection == dir,
      orElse: () => cameras[0],
    );
  } on CameraException catch (e) {
    if (kDebugMode) {
      debugPrint('getCamera error: ${e.code}');
    }
    return null;
  }
}