i have been trying to edit the name that the image picker uploads, from "image_picker123145468" to my own result like "image_for_idblabalbla"

but so far i cant find any thing related to it. hope someone here knows, ill be forever in your debt haha. thanks! here is the code i found in the internet.

chooseImage() {
    setState(() {
      // ignore: deprecated_member_use
      file = ImagePicker.pickImage(source: ImageSource.gallery);
    });
    setStatus('');
  }

  setStatus(String message) {
    setState(() {
      status = message;
    });
  }

  startUpload() {
    setStatus('Uploading Image...');
    if (null == tmpFile) {
      setStatus(errMessage);
      return;
    }
    String fileName = tmpFile.path.split('/').last;
    upload(fileName); //i think this should be the one that i need to edit but i dont know how.
  }

  upload(String fileName) {
    http.post(uploadEndPoint, body: {
      "image": base64Image,
      "name": fileName, //i tried manipulating this but no avail
    }).then((result) {
      setStatus(result.statusCode == 200 ? result.body : errMessage);
    }).catchError((error) {
      setStatus(error);
    });
  }


Solution 1: Sam Chan

You can use renameSync with a new path

import 'package:path/path.dart' as path;

And

file = ImagePicker.pickImage(source: ImageSource.gallery);    
String fileDir = path.dirname(file.path);
String newPath = path.join(fileDir, 'newFileName.FileFormat');
file .renameSync(newPath);


Solution 2: potatoprogrammer

Done some tinkering on the code , and somehow managed to change the name that is uploaded. Here is the code.

startUpload() {
    setStatus('Uploading Image...');
    if (null == tmpFile) {
      setStatus(errMessage);
      return;
    }
    String fileName = tmpFile.path.split("image_picker").last;
    String newFilename = fileName.replaceAll(fileName, 'changethistowhatever.jpg);
    upload(newFilename);

  }
  • "image_picker" in the path.split,this causes the default name image_picker1231.jpg to be reduced to 1231.jpg
  • "changethistowhatever.jpg" in the newfilename is used to rename the 1231.jpg to your customized name.