I'm would like to save an excel file that I generate on the fly in a flutter app. I want to save the file on the users device, which is going to be on a mobile. However, I'm trying to figure out where and how to save it.

This is what I have so far using this excel package from flutter:


// Some function code...

// Trying to save the file somewhere on a device
excel.encode().then((onValue) {
      ..createSync(recursive: true)

Anyone know the best way to do this?

Solution 1: eeqk

According to Read and write files cookbook: https://flutter.dev/docs/cookbook/persistence/reading-writing-files

you need to follow these steps:

  1. Find the correct local path.
    • include path_provider in pubspec.yaml
    • compute a path to destination parent directory, for example documents dir:
Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();
  return directory.path;
  1. Create a reference to the file location.
Future<File> get _localFile async {
  final path = await _localPath;
  return File('$path/filename.xlsx');
  1. Write data to the file.
Future<File> writeCounter(Excel excel) async {
  final file = await _localFile;
  return file.writeAsBytes(excel.encode());