Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter How to move file

I will move certain files. how to move the image file from directory to another directory, example file img.jpg from /storage/emulated/0/Myfolder to /storage/emulated/0/Urfolder?

like image 935
Dwi Nur Rohman Avatar asked Feb 14 '19 14:02

Dwi Nur Rohman


People also ask

How do I move files in flutter?

0" Then follow the steps: Run flutter upgrade in the terminal to upgrade Flutter Run dart migrate to run the dart migration tool. Solve all errors which the migration tool shows. Run flutter pub outdated --mode=null.

How do I move a file location?

You can move a file or folder from one folder to another by dragging it from its current location and dropping it into the destination folder, just as you would with a file on your desktop. Folder Tree: Right-click the file or folder you want, and from the menu that displays click Move or Copy.

How do I move files in console?

To move files, use the mv command (man mv), which is similar to the cp command, except that with mv the file is physically moved from one place to another, instead of being duplicated, as with cp.


2 Answers

await File('/storage/emulated/0/Myfolder').rename('/storage/emulated/0/Urfolder')

If the files are on different file systems you need to create a new destination file, read the source file and write the content into the destination file, then delete the source file.

like image 105
Günter Zöchbauer Avatar answered Oct 05 '22 11:10

Günter Zöchbauer


File.rename works only if source file and destination path are on the same file system, otherwise you will get a FileSystemException (OS Error: Cross-device link, errno = 18). Therefore it should be used for moving a file only if you are sure that the source file and the destination path are on the same file system.

For example when trying to move a file under the folder /storage/emulated/0/Android/data/ to a new path under the folder /data/user/0/com.my.app/cache/ will fail to FileSystemException.

Here is a small utility function for moving a file safely:

Future<File> moveFile(File sourceFile, String newPath) async {
  try {
    // prefer using rename as it is probably faster
    return await sourceFile.rename(newPath);
  } on FileSystemException catch (e) {
    // if rename fails, copy the source file and then delete it
    final newFile = await sourceFile.copy(newPath);
    await sourceFile.delete();
    return newFile;
  }
}
like image 29
kine Avatar answered Oct 05 '22 12:10

kine