Sub topic 1
image_picker or camera: which one do you need?
Flutter offers two common ways to get photos and videos into an app:
image_pickeropens a system gallery or camera application and returns the selected media.cameragives your app direct access to a camera, including a live preview and custom controls.
Both plugins can produce an XFile. An XFile is a cross-platform file reference that contains a path and methods for reading or copying the media. The main difference is who owns the capture screen: the operating system or your Flutter app.
Prerequisites
This chapter assumes that you know Dart async and await, Flutter widgets, StatefulWidget lifecycle methods such as initState and dispose, and how to add packages in pubspec.yaml.
Choose based on the user flow
Use image_picker when the user only needs to choose or quickly capture media. The plugin hands control to a familiar system screen, so your app needs less camera code and usually has fewer lifecycle problems.
Typical uses include:
- Selecting a profile image.
- Attaching an existing photo to a form.
- Choosing several images for a post.
- Taking a simple photo without custom camera controls.
- Selecting a video from the device.
Use camera when the camera is the main part of your interface. It provides a CameraController, a CameraPreview, and methods such as takePicture and startVideoRecording.
Typical uses include:
- A barcode or document scanner.
- A custom photo editor or framing experience.
- An app that needs flash, zoom, or camera switching controls.
- A screen that combines camera capture with Flutter widgets.
- Video recording with an in-app recording interface.
Compare the two plugins
| Requirement | `image_picker` | `camera` |
|---|---|---|
| System gallery selection | Built in | You must build the flow |
| Live preview in your UI | No | Yes |
| Custom flash, zoom, and camera controls | Limited to system UI | Yes |
| Implementation effort | Lower | Higher |
| Lifecycle management | Mostly handled by the system | Your responsibility |
The choice is about control versus implementation effort. For a profile-photo button, image_picker is usually the better fit. For a scanning screen where the user must align an object inside a frame, camera is the better fit.
Follow the decision flow
Common errors
Do not choose camera only because the app needs to take one photo. That adds controller and lifecycle code without adding useful control.
Do not assume that image_picker returns permanent storage. Its result may point to a temporary file, so copy or upload it when your workflow requires persistence.
Both plugins depend on platform permissions. A denied permission is a normal runtime outcome, not an exceptional condition that should crash the app. Test cancellation, denial, missing cameras, and interrupted app activity on real devices.
Exercise
You are building a receipt scanner that must show a framing rectangle, switch between front and rear cameras, and control the flash. Which plugin should you choose, and why?
Show answer
Choose camera, because the feature needs a live preview and controls inside the app. image_picker would delegate the capture screen to the system and would not provide the required custom interface.