Home > Software design >  renaming file before uploading it to firebase using firebase, flutter and image cropper
renaming file before uploading it to firebase using firebase, flutter and image cropper

Time:01-11

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.

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

CodePudding user response:

Try this:

final path = 'products/${imagePath.split("_").last}';
  • Related