Flutter: Internal Storage File could not delete

346 views Asked by At

I am trying to delete internal storage file into Flutter(Android). But It show error like this

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FileSystemException: Cannot delete file, path = '/storage/emulated/0/Pictures/Image Description (1).jpg' (OS Error: No such file or directory, errno = 2)

And I also check exitance of the file before deleting. Here is my code to delete file.

await Future.forEach(_selectedFile, (element) async {
      if (await element?.exists() ?? false) {
        await element?.delete();  // I got error in this line
      }
    });

I added all required permission to read and write internal storage.

Anyone have idea how to resolve it?

1

There are 1 answers

3
Cavin Macwan On

You can try this snippet:

Future<void> deleteFile(File file) async {
  try {
    if (await file.exists()) {
      await file.delete();
    }
  } catch (e) {
    // Error in getting access to the file.
  }
}