Starting with Flutter: File paths

theotherdevs

TheOtherDev/s

Posted on April 14, 2021

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

Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;

Directory applicationSupportDir = await getApplicationSupportDirectory();
String appSupportPath = applicationSupportDir.path;
Enter fullscreen mode Exit fullscreen mode

Use this directory to place user generated content. On iOS it uses the NSDocumentDirectoryand 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

Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
Enter fullscreen mode Exit fullscreen mode

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

if (Platform.isAndroid) {
  Directory externalStorage = await getExternalStorageDirectory();
  String externalStoragePath = externalStorage.path;
}
Enter fullscreen mode Exit fullscreen mode

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.

import 'package:path/path.dart' as p;

...
// Creates a path for a file in a directory
p.join('directory', 'file.txt');
// Splits the path in its components
List<String> pathComponents = p.split(path);
Enter fullscreen mode Exit fullscreen mode

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.

String dirName = p.dirname(path);
String baseName = p.basename(path);
String extension = p.extension(file_path);
Enter fullscreen mode Exit fullscreen mode

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

💖 💪 🙅 🚩
theotherdevs
TheOtherDev/s

Posted on April 14, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Flutter - Riverpod how to install it
flutter Flutter - Riverpod how to install it

January 20, 2022

Mastering Flutter: Custom Scrollview
flutter Mastering Flutter: Custom Scrollview

November 24, 2021

Starting with Flutter: File paths
flutter Starting with Flutter: File paths

April 14, 2021