Sub topic 1
image_picker or camera: which one do you need?
Flutter has two official plugins for getting photos and videos into an app. They solve different problems, and picking the wrong one costs you either weeks of extra work or a feature you can't build. This chapter gives you the working model of each plugin and a way to decide between them. Later chapters cover setup and code in detail.
Prerequisites
This chapter assumes you already know:
- Dart basics, including
async/awaitandFuture. - The Flutter widget model and the
StatefulWidgetlifecycle (initState,dispose). - How to add a package in
pubspec.yaml. - Roughly where the Android and iOS projects live in a Flutter app (
android/andios/).
The two plugins in one sentence each
- image_picker hands the job to the operating system. It opens the system gallery (or the system camera app), waits for the user to choose or shoot something, and returns the result to your app.
- camera gives your app direct control of the device camera. You show a live preview inside your own widget tree, decide when to capture, and manage the camera session yourself.
The key difference is who owns the screen. With image_picker, the OS owns it while the user picks. With camera, your widget owns it all the time.
How image_picker works
You call a method, the OS shows its own UI, and a Future completes when the user finishes. The result is an XFile, or null if the user cancelled.
import 'package:image_picker/image_picker.dart';
final ImagePicker _picker = ImagePicker();
Future<XFile?> pickFromGallery() {
return _picker.pickImage(
source: ImageSource.gallery,
maxWidth: 1024,
imageQuality: 85,
);
}The same plugin can launch the system camera app instead of the gallery:
Future<XFile?> takePhotoWithSystemCamera() {
return _picker.pickImage(source: ImageSource.camera);
}Things to note about this model:
- You never see a live preview. Your app is in the background while the system camera or gallery is in front.
- The user gets a familiar interface that the OS maintains, including accessibility and new device features.
- You can resize and compress images through options like
maxWidthandimageQuality. - You can select several images with
pickMultiImage, and you can pick video. - You cannot customize the picker's look, add overlays, or control the camera (zoom, flash, lens) beyond what the options expose.
How camera works
The camera plugin exposes the camera hardware as objects you manage. The basic sequence is:
- Call
availableCameras()to list the device cameras. - Create a
CameraControllerfor one of them and callinitialize(). - Show a
CameraPreviewwidget bound to that controller. - Call
takePicture()(or start video recording) when the user asks. - Call
dispose()on the controller when you're done.
import 'package:camera/camera.dart';
Future<XFile> captureOnce() async {
final cameras = await availableCameras();
final controller = CameraController(
cameras.first,
ResolutionPreset.medium,
enableAudio: false,
);
await controller.initialize();
try {
return await controller.takePicture();
} finally {
await controller.dispose();
}
}This sketch shows the flow, but a real screen can't initialize and dispose around a single capture like this, because the user needs the preview while they frame the shot. The full screen with a FutureBuilder, a preview, and a capture button is built in a later chapter.
What you get in exchange for the extra work:
- A live preview embedded anywhere in your layout, with your own buttons, overlays, and guides on top.
- Control over resolution, flash mode, zoom, focus, and which lens is active.
- Video recording that you start and stop yourself.
- Full responsibility for the lifecycle. A controller holds the hardware. If you forget to dispose it or you hold it while the app is in the background, you cause errors and drain the battery.
Both plugins return the same result type, XFile. Code that consumes a picked file (display, copy, upload) works unchanged whichever plugin produced it.
Side-by-side comparison
| Concern | image_picker | camera |
|---|---|---|
| Who draws the capture UI | The operating system | Your app |
| Live preview in your layout | No | Yes, via `CameraPreview` |
| Pick from the photo library | Yes | No |
| Multiple selection | Yes (`pickMultiImage`) | Not applicable |
| Capture a photo | Yes, through the system camera app | Yes, through `takePicture()` |
| Camera controls (zoom, flash, lens) | Very limited | Full |
| Lifecycle management | Almost none | You must handle it |
| Code you write | A few lines | A screen, a controller, error handling |
| Result type | `XFile` | `XFile` |
Note that the camera plugin does not read the photo library. If users must choose existing photos, you need image_picker regardless.
A decision procedure
Ask these questions in order.
- Must the user be able to choose existing photos or videos? Use image_picker. The camera plugin cannot do this.
- Does the camera experience need to live inside your own UI? Examples are a document scanner with an edge overlay, a QR frame, a selfie guide, or a custom shutter. Use camera.
- Do you need programmatic control of flash, zoom, focus, or resolution? Use camera.
- Otherwise, do you just need a photo or file in your app? Use image_picker. It is less code and the OS handles edge cases.
Using both is common. A profile screen might offer "Choose from library" through image_picker and "Take photo" through a custom camera screen, and both paths feed the same XFile handling code.
Platform and permission differences
Both plugins need platform configuration, covered in the next chapter. The differences to keep in mind now:
- iOS requires usage-description strings in
Info.plistfor any camera, photo library, or microphone access. Without them, the call fails or the app crashes. - image_picker delegates most permission handling to the OS pickers. A user who denies access usually sees a failed or empty result rather than a crash you caused.
- camera asks for permission when you call
initialize(). A denial surfaces as aCameraExceptionthat you must catch and turn into a useful screen.
import 'package:camera/camera.dart';
import 'package:flutter/foundation.dart';
Future<void> safeInit(CameraController controller) async {
try {
await controller.initialize();
} on CameraException catch (e) {
debugPrint('Camera error ${e.code}: ${e.description}');
}
}The camera approach therefore asks more of you: handling denial, lifecycle changes, and cleanup are your job.
Common mistakes when choosing
- Building a custom camera screen for a plain "attach photo" button. You take on lifecycle bugs and permission handling for no user benefit.
- Expecting image_picker to give a live preview. It never will. If a design shows a camera view inside a page, you need camera.
- Expecting camera to open the gallery. It can't. Add image_picker for that path.
- Testing only on an emulator or simulator. Camera behavior differs on real hardware. Later chapters cover device verification.
- Treating a
nullresult as an error. For image_picker,nullmeans the user cancelled, which is a normal outcome.
Adding the packages
Add whichever plugins you chose. Use the current versions listed on pub.dev rather than copying a version number from an article.
dependencies:
flutter:
sdk: flutter
image_picker: ^1.0.0
camera: ^0.10.0You can also add them from the command line with flutter pub add image_picker camera, which picks compatible versions for your project.
What comes next
The following chapters configure Android and iOS, explain XFile, and then walk through image_picker in depth before building a full CameraController screen. The choice you make here decides which of those chapters you will lean on most.
Exercise
A recipe app lets users attach a photo to a recipe. They can choose an existing photo or take a new one, and the app does not need a live preview, zoom, or custom overlays. Which plugin or plugins should you use, and why? Then name one requirement that would make you add the other plugin.
Show answer
Use image_picker alone. It covers both gallery selection (ImageSource.gallery) and capture through the system camera app (ImageSource.camera), with far less code and no lifecycle management. Adding camera becomes necessary if the design requires a live preview inside your own UI, such as a custom shutter button, an overlay guide, or programmatic control of flash and zoom.
Exercise
Your teammate wants to use only the camera plugin for a "choose a profile picture" feature, because it returns an XFile. What is the problem with that plan?
Show answer
The camera plugin only captures from the device cameras. It cannot open the photo library, so users could not choose an existing photo. image_picker is needed for that path. Both plugins return XFile, so the code that handles the result stays the same.