Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a file from the local storage in Flutter?

Actually i want to overwrite the file in local storage of phone but it gives me the error

    I/flutter ( 3835): /storage/emulated/0/Android/data/com.example.temp/files/flutter_audio_recorder_
I/flutter ( 3835): Exception: A file already exists at the path :/storage/emulated/0/Android/data/com.example.temp/files/flutter_audio_recorder_.wav

so how can i delete this file first ?

i have stored the wav file path in this variable ==> var dirPath ;

like image 951
Muhammad Tameem Rafay Avatar asked May 10 '20 11:05

Muhammad Tameem Rafay


1 Answers

Create this method:

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

Usage:

deleteFile(File('your_file_path'));
like image 168
CopsOnRoad Avatar answered Sep 23 '22 14:09

CopsOnRoad