Funções para trabalhar com arquivos em apps dart e flutter

  final String logName = 'main';
  final Directory cacheDirectory = Constants.cacheDirectory;

  final String imageUrl = 'https://upload.wikimedia.org/wikipedia/commons/1/17/Google-flutter-logo.png';
  final String fileName = 'Google-flutter-logo.png';
  final String filePath = '${cacheDirectory.path}${Constants.pathSeparator}$fileName';
  log('Cache directory path: ${cacheDirectory.path}', name: logName);



  final bool exists = await Functions
    .checkIfTheFileExists('${cacheDirectory.path}${Constants.pathSeparator}Google-flutter-logo.png');
  log('exist: $exists', name: logName);



  await Functions.getBytesFromInternet(imageUrl)
    .then((Uint8List bytes) {
      Functions.createFile(path: filePath, bytes: bytes).then((value) {
        log(
          '---- Arquivo armazenado com sucesso ----',
          name: logName,
        );
      });
  });



  await Functions.getBytesFromFile(filePath).then((value) {
    // ...
  });



  // Armazenar arquivo em cache
  await const FilesCache().getBytes(
    imageUrl, 
    duration: const Duration(days: 3),).then((Uint8List bytes) {
    log(
      'Size of bytes: ${bytes.lengthInBytes}',
      name: logName,
    );
  });



  // Deletar tudo
  await FilesCache.deleteAll();



  // Verificar o tamanho total do arquivos armazenado em cache
  final num bytes = await FilesCache.getCacheFolderSizeFromJson();
  late final String size;
  if (bytes >= 1e+9) {
    size = '${(bytes / 1e+9).toStringAsFixed(3)} GB';
  } else {
    size = '${(bytes / 1e+6).toStringAsFixed(3)} MB';
  }
  log('Total size of cached files: $size', name: logName);



  // Verificar e remover os arquivos expirados
  await FilesCache.checkForAndRemoveExpiredFiles();

Libraries

files_cache