appendDataToFile method

Future<void> appendDataToFile(
  1. dynamic data,
  2. DataType dataType, {
  3. String? fileName,
  4. String? path,
})

Use for app append data to a specific file on disc.

  • If path is not provid it will seach the file in the application directory named files.
  • If path (entire Lunix or windows path) is provide, fileName must be null
  • If DataType (type of the data we want to save) equals DataType.text it will append the given string to the text file
    else it appends the data as bytes to the file

Implementation

Future<void> appendDataToFile(var data, DataType dataType,
    {String? fileName, String? path}) async {
  File fileToSave =
      File(validatePath(path) ?? "${await filesPath}$pathJoin$fileName");
  if (data != null && data.isNotEmpty && fileToSave.existsSync()) {
    switch (dataType) {
      case DataType.text:
        fileToSave.writeAsStringSync(data, mode: FileMode.append);
        break;
      case DataType.base64:
      case DataType.bytes:
        if (dataType == DataType.base64) data = base64Decode(data);
        fileToSave.writeAsBytesSync(data, mode: FileMode.append);
        break;
    }
  }
}