pickImage static method

Future<String?> pickImage({
  1. ImageSource source = ImageSource.gallery,
  2. required void onError(
    1. String e
    ),
})

Picks a single image from the specified ImageSource.

Returns the file path of the picked image or null if no image is selected. Calls onError in case of an error.

Implementation

static Future<String?> pickImage({
  ImageSource source = ImageSource.gallery,
  required void Function(String e) onError,
}) async {
  try {
    final pickedFile = await _imagePicker.pickImage(
      source: source == ImageSource.camera
          ? image_picker.ImageSource.camera
          : image_picker.ImageSource.gallery,
    );
    if (pickedFile == null) return null;

    final path = pickedFile.path;
    return path;
  } catch (e) {
    onError(e.toString());
    return null;
  }
}