Image Picker is a Flutter plugin used to select images or videos from the device gallery or capture new ones using the camera. It is commonly used for features such as profile pictures, media attachments, or content uploads.
The most widely used implementation is provided by the image_picker package, which offers a simple API and official support for mobile and web platforms.
The plugin functions as a bridge between the Dart runtime and the operating system's native intent system or media controllers.
Intent that delegates the task to a camera app or gallery selector chosen by the user.UIImagePickerController or PHPickerViewController over the Flutter view. Once a user selects or captures media, the native layer writes the data to a temporary file in the app's cache directory and passes the file path back to Dart via a Platform Channel. This means the file exists locally on the device immediately after selection, but since it is stored in a temporary location, developers must persist it or upload it before the OS cleans up the cache.
A basic example of picking a single image from the gallery:
final picker = ImagePicker();
Future<XFile?> pickImage() async {
return await picker.pickImage(
source: ImageSource.gallery,
);
}The returned file can then be displayed in the UI, uploaded to a backend, or processed further.
Image Picker supports selecting multiple images using pickMultiImage.
final picker = ImagePicker();
Future<List<XFile>> pickMultipleImages() async {
return await picker.pickMultiImage();
}This is commonly used for features like multi-image uploads or gallery selection. Support is stable on Android, iOS, and web, but exact behavior depends on platform capabilities.
On Android, opening the camera may cause the system to temporarily destroy the application’s MainActivity to free memory. This is especially common on older or low-memory devices. If this happens, the selected image can be lost when the user returns to the app, even though the photo was successfully taken.
To handle this correctly, Image Picker provides the retrieveLostData method, which allows recovering the image after the activity is recreated. Failing to handle this case results in an implementation that works only on the happy path and breaks for a subset of Android users.
On iOS, Image Picker requires explicit permission configuration. You must add usage descriptions to Info.plist for photo library and camera access. Without these entries, the picker will fail at runtime. iOS enforces strict privacy rules, making correct permission configuration mandatory even for simple image selection scenarios.
On web, Image Picker opens a browser file selection dialog.
Important characteristics include:
XFile objects.Web support is reliable but more limited compared to Android and iOS.
Image Picker provides built-in parameters for basic image optimization.
Options such as maxWidth, maxHeight, and imageQuality allow reducing image size before uploading it to a server. This is often sufficient to reduce file size from several megabytes to a few hundred kilobytes. Using these options is the simplest way to optimize uploads without introducing additional image processing libraries.
Most problems originate from platform configuration or lifecycle handling rather than the Flutter API itself.
retrieveLostData ensures that selected images are recovered if the app is terminated or the activity is recreated while the picker was open. This prevents user frustration and data loss in real-world scenarios.AndroidManifest.xml and Info.plist) and provide clear explanations to users.8 min. • Nov 8, 2022
Almost every production-grade app depends on tens of packages. Using them allows for quick implementation of standard functionalities. Take a closer look at 6 useful but not-so-popular Dart and Flutter packages made and tested by LeanCode's devs.
8 min. • Aug 8, 2022
One of the projects we delivered at LeanCode was building an application for Healthcare Industry - Quittercheck. While developing the app, the problem we faced was how to limit a video upload to a specific time frame. Read about our tailored solution to this challenge.