Search code examples
flutterdartcopyfile-copyingflutter-desktop

Can't copy the file after build on flutter desktop application


in my project I can press the save button to copy a file inside flutter in debug mode and every thing works fine (the file copied), but after build and open the release application I can't copy the file with no error message, just the button pressed and nothing happened.

I tried to run the release application as administrator, but it's the same problem.

 Widget buildButton() {
final isFormValid = name.isNotEmpty && path.isNotEmpty;

return Padding(
  padding: EdgeInsets.symmetric(vertical: 8, horizontal: 12),
  child: ElevatedButton(
    style: ElevatedButton.styleFrom(
      onPrimary: Colors.white,
      primary: isFormValid ? null : Colors.grey.shade700,
    ),
    onPressed: () async {
      addOrUpdatePlan();
      File PDF= File('assets/PDF/file.pdf');
      File newPDF = await PDF.copy('$path/$name.pdf'); //////The Bug
    },
    child: Text(
      'SAVE',
      style: TextStyle(fontSize: 30),
    ),
  ),
);

}


Solution

  • You are using a relative path in your File constructor; that means that it will only work if the current working directory when the application is run happens to be the directory that the path is relative to. The working directory of an application is not controlled by where the application is, but how it was launched, so is not within your control as an application developer.

    You should always use absolute paths, not relative paths, when writing applications. How you construct an absolute path for your use case depends on what the intended base directory is.