pickCropAndUploadToFirebaseStorage static method

Future<String?> pickCropAndUploadToFirebaseStorage(
  1. BuildContext context, {
  2. ImageSource source = ImageSource.gallery,
  3. required String storagePath,
  4. List<CropAspectRatioPreset> aspectRatioPresets = const [CropAspectRatioPreset.square],
  5. CropStyle cropStyle = CropStyle.rectangle,
  6. Size compressSize = const Size(720, 1280),
  7. required void onError(
    1. String e
    ),
})

Picks an image, crops it according to the specified style, and uploads it to Firebase Storage.

The context is used to show the cropping UI and handle navigation. The source determines whether the image is picked from the gallery or camera (default is ImageSource.gallery). The storagePath specifies the directory in Firebase Storage where the image will be uploaded. The aspectRatioPresets defines a list of aspect ratios that can be used for cropping (default is CropAspectRatioPreset.square). The cropStyle determines whether the image is cropped as a rectangle or circle (default is CropStyle.rectangle). The compressSize defines the target dimensions for compressing the image (default is 720x1280). The onError callback is invoked in case of any error, passing the error message as a string.

Returns the download URL of the uploaded image if successful, or null if any step fails.

Implementation

static Future<String?> pickCropAndUploadToFirebaseStorage(
  BuildContext context, {
  ImageSource source = ImageSource.gallery,
  required String storagePath,
  List<CropAspectRatioPreset> aspectRatioPresets = const [
    CropAspectRatioPreset.square
  ],
  CropStyle cropStyle = CropStyle.rectangle,
  Size compressSize = const Size(720, 1280),
  required void Function(String e) onError,
}) async {
  try {
    final path = await pickImage(
      source: source,
      onError: onError,
    );
    if (path == null) return null;

    final cropPath = await cropImage(
      context,
      path: path,
      aspectRatioPresets: aspectRatioPresets,
      cropStyle: cropStyle,
      onError: onError,
    );
    if (cropPath == null) return null;

    final url = await uploadToFirebaseStorage(
      compressSize: compressSize,
      storagePath: storagePath,
      filepath: cropPath,
      onError: onError,
    );
    return url;
  } catch (e) {
    onError(e.toString());
    return null;
  }
}