writeToCSV method

Future<File> writeToCSV(
  1. List<DataClass> table, {
  2. String csvFileName = 'table',
  3. String? overrideFilePath,
})

Final writing is done by this member.

Pass table is a List

csvFileName is the file name that generated CSV is to be stored as

overrideFilePath is the path to the directory where the CSV is to be stored. If not provided, the default path is used based on Platform

Returns a Future<File> that can be used to perform further operations on the generated CSV file.

Implementation

Future<File> writeToCSV(
  List<DataClass> table, {
  String csvFileName = 'table',
  String? overrideFilePath,
}) async {
  // Generate the body of the CSV from the passed List<DataClass> _table
  final String csvBody = _generateCSVBody(table);

  // Write the file
  final File? thisFile = await _localFile(
    csvFileName,
    overrideFilePath: overrideFilePath,
  );

  // Generate File pointer.
  // Occurs when file name is not proper
  if (thisFile == null) {
    throw PlatformException(
      code: '404',
      message: 'File Save path not found',
    );
  }

  // If permission is not given, try getting it again.
  if (!hasFileWritePermission) {
    await getPermission();
  }

  // If permission is still not given, throw error.
  if (!hasFileWritePermission) {
    throw new PlatformException(
      code: '401',
      message: 'Permission not granted',
    );
  }

  return await thisFile.writeAsString(csvBody);
}