Search code examples
flutterflutter-downloaderpath-provider

Downloading file from Flutter app to mobile phone


I am trying to export an excel file from my flutter app to my mobile. It exports the file to "Excel file exported to: /data/user/0/com.example.bmc/app_flutter/table_data.xlsx". When I check my File Manager, the file is nowhere, however, if i connect my phone with laptop, the file is saved in "Android > Data > com.example.bmc > Data > Files > table_data.xlsx" which is not visibile on my mobile phone.

I am trying to export an excel file from my flutter app to my mobile. It exports the file to "Excel file exported to: /data/user/0/com.example.bmc/app_flutter/table_data.xlsx". When I check my File Manager, the file is nowhere, however, if i connect my phone with laptop, the file is saved in "Android > Data > com.example.bmc > Data > Files > table_data.xlsx" which is not visibile on my mobile phone.

I tried various packages such as "path_provider", "downloader_manager" and many more.I was expecting to see the exported file in my Documents dolder of my phone.

Future<void> _exportToExcel() async {
    try {
      final Excel excel = Excel.createExcel();
      Sheet sheetObject = excel['Sheet1'];

      // Add headers to Excel file
      for (int colIndex = 0; colIndex < 10; colIndex++) {
        sheetObject.cell(CellIndex.indexByColumnRow(columnIndex: colIndex, rowIndex: 0)).value = 'Column $colIndex';
      }

      // Add data to Excel file
      for (int rowIndex = 0; rowIndex < 20; rowIndex++) {
        for (int colIndex = 0; colIndex < 10; colIndex++) {
          sheetObject.cell(CellIndex.indexByColumnRow(columnIndex: colIndex, rowIndex: rowIndex + 1)).value = tableData[rowIndex][colIndex];
        }
      }
      final String documentsDirectory = (await getApplicationDocumentsDirectory()).path;
      final String filePath = '$documentsDirectory/table_data.xlsx';
      // Save the Excel file
      List<int>? excelBytes = excel.encode();
      await OpenFile.open(filePath);
      print('Excel file exported to: $filePath');
    } catch (e) {
      print('Error exporting to Excel: $e');
    }
  }

Solution

  • Use external_path package and add below snippet to get Document directory

    final path = await ExternalPath.getExternalStoragePublicDirectory(
                      ExternalPath.DIRECTORY_DOCUMENTS);