getImageFromSource method
- required ImageSource source,
- ImagePickerOptions options = const ImagePickerOptions(),
Returns an XFile with the image that was picked.
The source argument controls where the image comes from. This can
be either ImageSource.camera or ImageSource.gallery.
The options argument controls additional settings that can be used when
picking an image. See ImagePickerOptions for more details.
Where iOS supports HEIC images, Android 8 and below doesn't. Android 9 and
above only support HEIC images if used in addition to a size modification,
of which the usage is explained in ImagePickerOptions.
In Android, the MainActivity can be destroyed for various reasons. If that happens, the result will be lost in this call. You can then call getLostData when your app relaunches to retrieve the lost data.
If no images were picked, the return value is null.
Implementation
@override
Future<XFile?> getImageFromSource({
required ImageSource source,
ImagePickerOptions options = const ImagePickerOptions(),
}) async {
switch (source) {
case ImageSource.camera:
return super.getImageFromSource(source: source);
case ImageSource.gallery:
// TODO(stuartmorgan): Add a native implementation that can use
// PHPickerViewController on macOS 13+, with this as a fallback for
// older OS versions: https://github.com/flutter/flutter/issues/125829.
const typeGroup = XTypeGroup(
uniformTypeIdentifiers: <String>['public.image'],
);
final XFile? file = await fileSelector.openFile(
acceptedTypeGroups: <XTypeGroup>[typeGroup],
);
return file;
}
// Ensure that there's a fallback in case a new source is added.
// ignore: dead_code
throw UnimplementedError('Unknown ImageSource: $source');
}