Sub topic 8
Listing the Device Cameras
Assume you know Dart async/await, Future, StatefulWidget, and how to add a package to pubspec.yaml. This chapter focuses on discovering cameras. Camera permissions and platform configuration must already be in place.
The camera plugin represents a physical camera with a CameraDescription. A description contains metadata such as the camera name, whether it faces the front or back, and its sensor orientation. It does not open the camera or show a preview.
Call availableCameras() to ask the platform for the cameras it can use:
import 'package:camera/camera.dart';
Future<List<CameraDescription>> discoverCameras() async {
try {
return await availableCameras();
} on CameraException catch (error) {
throw StateError(
'Could not discover cameras: ${error.code} ${error.description}',
);
}
}availableCameras() returns a Future because the plugin communicates with Android or iOS. Do not call it repeatedly inside build. Store the future in state or load the result in initState.
Displaying camera metadata
This screen discovers the cameras once and lists their important properties:
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';
class CameraListScreen extends StatefulWidget {
const CameraListScreen({super.key});
@override
State<CameraListScreen> createState() => _CameraListScreenState();
}
class _CameraListScreenState extends State<CameraListScreen> {
late final Future<List<CameraDescription>> _camerasFuture;
@override
void initState() {
super.initState();
_camerasFuture = availableCameras();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Available cameras')),
body: FutureBuilder<List<CameraDescription>>(
future: _camerasFuture,
builder: (context, snapshot) {
if (snapshot.connectionState != ConnectionState.done) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return Center(
child: Text('Camera discovery failed: ${snapshot.error}'),
);
}
final cameras = snapshot.data ?? [];
if (cameras.isEmpty) {
return const Center(child: Text('No cameras found'));
}
return ListView.builder(
itemCount: cameras.length,
itemBuilder: (context, index) {
final camera = cameras[index];
return ListTile(
title: Text(camera.name),
subtitle: Text(
'${camera.lensDirection.name} lens, '
'${camera.sensorOrientation}° sensor',
),
);
},
);
},
),
);
}
}The list may contain several descriptions. A phone often has front, rear, wide-angle, and telephoto cameras. The exact names and ordering vary by device, so do not select a camera by assuming that index 0 is always the rear camera.
Choosing a camera
Select by lensDirection when you need a general front or rear camera:
CameraDescription? findRearCamera(
List<CameraDescription> cameras,
) {
for (final camera in cameras) {
if (camera.lensDirection == CameraLensDirection.back) {
return camera;
}
}
return null;
}Pass the selected CameraDescription to CameraController in the next step. Discovery and controller initialization are separate operations.
Exercise
Why should an app select a camera by lensDirection instead of assuming that cameras[0] is the rear camera?
Show answer
Camera ordering and the number of cameras vary between devices. lensDirection expresses the intended front or rear position.