Starting with Flutter: File paths
Tags: flutter, tutorial, beginners, dart
Published: 2021-04-14 15:03:53

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](https://pub.dev/packages/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

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

Directory applicationSupportDir = await getApplicationSupportDirectory();
String appSupportPath = applicationSupportDir.path;
```


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

```dart
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
```

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

```dart
if (Platform.isAndroid) {
  Directory externalStorage = await getExternalStorageDirectory();
  String externalStoragePath = externalStorage.path;
}
```

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](https://pub.dev/packages/path). It gives you simple commands to manipulate file and directory paths, here's some examples.

```dart
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);
```

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. 

```dart
String dirName = p.dirname(path);
String baseName = p.basename(path);
String extension = p.extension(file_path);
```

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