【问题标题】:How to delete a file from the local storage in Flutter?如何从 Flutter 的本地存储中删除文件?
【发布时间】:2020-08-25 21:24:46
【问题描述】:

实际上我想覆盖手机本地存储中的文件,但它给了我错误

    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

那么我怎样才能先删除这个文件呢?

我已将 wav 文件路径存储在此变量中 ==> var dirPath ;

【问题讨论】:

    标签: flutter flutter-dependencies


    【解决方案1】:

    创建这个方法:

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

    用法:

    deleteFile(File('your_file_path'));
    

    【讨论】:

    • 是的,它工作正常确保你导入这个文件 import 'dart:io' as io; ------ > 如果将此文件作为 io 导入,则应将其写为 var file = io.File('file_path');
    猜你喜欢
    • 1970-01-01
    • 2016-07-03
    • 2020-05-10
    • 2018-11-19
    • 2020-10-31
    • 2011-12-25
    • 2021-07-02
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多