Starting with Flutter: File paths

You’ve downloaded a beautiful image and now you’re asking yourself: where should I save it? This tutorial is for you, we will discover the path_provider package, that will give you access to the commonly used location in the file system. path_provider supports iOS, Android, Linux and MacOS but we will focus on the 2 mobile platforms. 

Application Documents Directory

Use this directory to place user generated content. On iOS it uses the NSDocumentDirectory and on Android uses the getDataDirectory API on the context.
If the data is not user generated you should use the Application Support Directory that uses the NSApplicationSupportDirectory on iOS and the getFilesDir API on the context on Android.

Temporary Directory

Use this directory to store temporary data (caches, temporary downloaded files). This directory is not backed up and its content could be deleted if the system needs storage space. 
On iOS, this uses the NSCachesDirectory API. On Android, this uses the getCacheDir API on the context.

External Storage Directory

Use this function to retrieve the external storage directory.
This directory is available only on Android (if you try to call this function on iOS it will throw an UnsupportedError) and uses the getExternalFilesDir(null).

Path 

Another package that will come handy if you’re dealing with file paths and directories is, of course, the path package. It gives you simple commands to manipulate file and directory paths, here’s some examples.

As you can see the package is intended to be imported with a prefix (the “as p” in the code after the import), so you will use its functions with p.function().

The join function is used to join a path with a file name, it will return the complete path of the file.

The split function does the opposite, splits the various components of the paths in its subcomponents, so this path “/test/directory/filename” will result in this list of strings [“test”, “dierctory”, “filename”]

The path package is very useful if you need to extract the name of a file from a path, or its extension.

For example if you use this path “/test/directory/filename.txt” the above code will produce:
dirName = /test/directory
baseName = filename.txt
extension = txt