Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save a file to a user specific directory flutter Desktop?

Tags:

flutter

dart

How can I allow the user to save a file to a specific folder in Flutter? I have built a simple desktop app for Mac that returns a file from an API. Currently, it saves the file to a caches directory.

try {
  io.Directory saveDir = await getTemporaryDirectory();
  String filePath = saveDir.path;
  io.File returnedFile = new io.File('$filePath/$filename.xlsx');
  await returnedFile.writeAsBytes(result.bodyBytes);
  print(saveDir);
} catch (e) {
  print(e);
}
like image 985
Stephen Boikanyo Avatar asked Oct 19 '25 12:10

Stephen Boikanyo


1 Answers

I played around with my original code and that provided by @Pavel and managed to write my solution to saving files to a custom user-picked directory in this fashion.

The first part opens a file picker dialogue that returns a path to a directory.

The second part provides the path to the File class that then writes the file to that directory. Hope this helps anyone trying to save files.

String? outputFile = await FilePicker.platform.saveFile(
                      dialogTitle: 'Save Your File to desired location',
                      fileName: filename);
                      
                     
                  try {
                    io.File returnedFile = io.File('$outputFile');
                    await returnedFile.writeAsBytes(responsefile.bodyBytes);
                  } catch (e) {}
   
like image 78
Stephen Boikanyo Avatar answered Oct 21 '25 03:10

Stephen Boikanyo