Flutter Saving CSV to Android and iOS Filesystem

50 Views Asked by At

I have been reading a number of different Stackoverflow answers that say to use the path_provider plugin. I think I have everything workign fine but its not quite saving.

 // Convert the data to CSV format
                    String csvContent =
                        const ListToCsvConverter().convert(csvData);

                    // Write the CSV content to a file

                    final Directory? appDocumentsDir =
                        await getApplicationDocumentsDirectory();

                    // Ensure the downloads directory is not null
                    if (appDocumentsDir != null) {
                      // Create a file in the downloads directory
                      File file =
                          File('${appDocumentsDir.path}/your_file_name.csv');

                      // Write the CSV content to the file
                      await file.writeAsString(csvContent);

                      print('Data exported to CSV file: ${file.path}');
                    } else {
                      print('Error: Unable to get the downloads directory.');
                    }

Whats odd is when I print the location it is saving too I get this:

/data/user/0/com.appName/app_flutter/your_file_name.csv

Which is not the documents directory as this line should be grabbing:

final Directory? appDocumentsDir = await getApplicationDocumentsDirectory();

When I look in the documents directory on my emulator I dont see any files. So I dont think its saving properly.

0

There are 0 best solutions below