0

I'm trying with two different images with same name. But the path was same for two picked images. it was not unique.So that I uploaded in server second image with the same name of first uploaded image. But server had both the image are same and it had first image. So how to handle this case and customize the path?

Maheswari
  • 65
  • 1
  • 6

1 Answers1

0

You can use the path_provider to define the customize directory on your app.

So, copy the file with your customize path and rename the file name.

BTW, do NOT save the absolute path of File on iOS. The iOS use SandBox to access the file. When you get the file path every time. The file path will be different.

class FileUtils {
    final String avatarPath = '/avatar/';

    Future<String> getAvatarDirectoryPath() async {
        final String appDirPath = await getApplicationSupportDirectory().path;
        final Directory avatarDirPath = await Directory(appDirPath + avatarPath).create();
        return directory.path;
    }
}

// Example
{
    final XFile? image = await ImagePicker().pickImage(source: ImageSource.camera);
    final File imageFile = File(image.path);
    final File newFile = File(await FileUtils().getAvatarDirectoryPath() + 'userAvatar.png');
    await imageFile.copy(newFile.path);
}
Shun Min Chang
  • 868
  • 7
  • 13