【问题标题】:Flutter: moving files from local machineFlutter:从本地机器移动文件
【发布时间】:2022-11-29 14:24:48
【问题描述】:

你可以使用 flutter 从本地机器移动文件吗?例如从C:\images\photo.pngC:\photos\photo.png

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    是的,你可以使用 Dart/Flutter 移动文件,你需要导入 dart:io

    import "dart:io";
    

    那么你可以使用这个方法,你可以理解它用我写的cmets做了什么。

     Future<File> moveFile(File originalFile, String targetPath) async {
      try {
        // This will try first to just rename the file if they are on the same directory, 
        return await originalFile.rename(targetPath);
    
      } on FileSystemException catch (e) {
        // if the rename method fails, it will copy the original file to the new directory and then delete the original file
        final newFileInTargetPath = await originalFile.copy(targetPath);
        await originalFile.delete();
        return newFileInTargetPath;
      }
    }
        
      final file = File("C:/images/photo.png");
      final path = "C:/photos/";
      await moveFile(file, path);
    

    但是,我将在这里解释它的作用:

    如果你的文件在同一路径目录下,那么就不需要移动它,只需用rename()方法重命名即可,如果文件在你系统的其他目录下,它将创建一个新的@987654325 @ 它将那个文件复制到那个路径,现在我们将有两个副本 File,一个在旧路径下,另一个在新路径下,所以我们需要用 delete() 方法删除旧的,最后我们用return newFile;返回了新文件

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 2011-12-05
      • 2014-08-18
      • 2013-01-24
      • 2018-05-23
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      相关资源
      最近更新 更多