5

I am using a flutter plugin named path_provider. I have to store image file at path_provider.getTemporaryDirectory(). Is the image stored here is deleted automatically or I have to do it explicitly.

1
  • If you know any way of cleaning this temporary directory within program then please provide the solution to me. Thank you. Jul 5, 2019 at 5:47

2 Answers 2

14

from the documentation of path_provider

Files in this directory may be cleared at any time. This does not return a new temporary directory. Instead, the caller is responsible for creating (and cleaning up) files or directories within this directory. This directory is scoped to the calling application.

So you are responsible for cleaning up, which means it is not automatically cleared, but it may be cleared any time

Edit

You can clear the temporary the directory as follows:

import 'dart:io';
....
Directory dir = await getTemporaryDirectory();
dir.deleteSync(recursive: true);
dir.create(); // This will create the temporary directory again. So temporary files will only be deleted
2
  • If you know any way of cleaning this temporary directory within program then please provide the solution to me. Thank you. Jul 5, 2019 at 5:48
  • 6
    This will also delete the director which can lead to bad things... May 8, 2021 at 18:21
0

To keep things safe, I store all the file paths in list with flutter_secure_storage, then when I launch the app I browse all the file paths, check if file still exists (can be previously deleted by system) and delete it. Finally, I clear the list from flutter_secure_storage for next time.

AppSecureStorage.dart

import 'dart:convert';

import 'package:flutter_secure_storage/flutter_secure_storage.dart';

class AppSecureStorage {
  static const _storage = FlutterSecureStorage();
  static const TEMP_FILES_PATH = 'temp-files-path';

  static Future<String?> get(String key) async {
    return await _storage.read(key: key);
  }

  static Future<void> set(String key, String? value) async {
    if (value == null) {
      await _storage.delete(key: key);
    } else {
      await _storage.write(
        key: key,
        value: value,
      );
    }
  }

  static Future<List<String>> getTempFilesPath() async {
    final tempFilesPathValue = await get(
      AppSecureStorage.TEMP_FILES_PATH,
    );

    List<String> tempFilesPathStrList = [];

    if (tempFilesPathValue != null) {
      List<dynamic> tempFilesPathDynamicList = jsonDecode(tempFilesPathValue);
      for (var element in tempFilesPathDynamicList) {
        tempFilesPathStrList.add(element.toString());
      }
    }

    return tempFilesPathStrList;
  }

  static Future<void> addTempFile(String filePath) async {
    final tempFilesPath = await AppSecureStorage.getTempFilesPath();
    tempFilesPath.add(filePath);

    AppSecureStorage.set(
      AppSecureStorage.TEMP_FILES_PATH,
      jsonEncode(tempFilesPath),
    );
  }
}

main.dart

void _handleTempFiles() async {
    List<String> tempFilesPath = await AppSecureStorage.getTempFilesPath();

    for (var element in tempFilesPath) {
      final file = File(element);
      // check if still exist : tmp file can be deleted by system
      if (await file.exists()) {
        file.delete();
      }
    }

    AppSecureStorage.set(AppSecureStorage.TEMP_FILES_PATH, jsonEncode([]));
  }
1
  • Hi mate, can you share the code please? Jan 8 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.