Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

renaming file before uploading it to firebase using firebase, flutter and image cropper

Good Day. I would like to ask on how do you rename your file(image) before uploading it in your firebase storage?

I am using image_cropper when selecting and cropping my images. However I cannot see if it has any built-in function for renaming files.

This is my selectFile when selecting images in the local device.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Future selectFile() async {
    try {
      final image = await ImagePicker().pickImage(source: ImageSource.gallery);
      if (image != null) {
        final croppedImage = await ImageCropper().cropImage(
          sourcePath: image.path,
          aspectRatio: const CropAspectRatio(ratioX: 1, ratioY: 1),
          compressQuality: 100,
          maxWidth: 450,
          maxHeight: 450,
          compressFormat: ImageCompressFormat.jpg,
          aspectRatioPresets: [CropAspectRatioPreset.square],
        );
        setState(() {
          imagePath = croppedImage!.path;
        });
      }
    } on PlatformException catch (e) {
      print(e);
      Navigator.of(context).pop();
    }
  }

and this is my uploadFile function

Future uploadFile() async {
    final path = 'products/${imagePath.split("/").last}';
    
    final file = File(imagePath);

    final ref = FirebaseStorage.instance.ref().child(path);
    uploadTask = ref.putFile(file);

    final snapshot = await uploadTask!.whenComplete(() {});

    final urlDownload = await snapshot.ref.getDownloadURL();
    var url = urlDownload.toString();
    print(url);

the imagePath is my selected image.

and every time I upload it on my firebase storage it renames itself as
image_cropper_1234567890.jpg and I just want to display the image as 1234567890

>Solution :

Try this:

final path = 'products/${imagePath.split("_").last}';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading