Sub topic 4
Finding the Right Directory with `path_provider`
Assume you know basic Dart classes, null safety, and async/await. This chapter also assumes you can add packages to pubspec.yaml. The goal is to choose a platform-appropriate directory before reading or writing files.
A Flutter app should not guess a device path such as /storage/emulated/0/app_data. Android, iOS, macOS, Windows, and Linux use different directory layouts. The path_provider package asks the operating system for directories intended for specific purposes.
Directory choices
| Directory | Best use | Important behavior |
|---|---|---|
| Application documents | User-created or user-important files | Usually included in app backups |
| Application support | App-managed data that users do not edit directly | Intended for persistent internal files |
| Temporary | Caches and disposable files | The operating system may delete contents |
| External storage | Shared user-visible files on supported platforms | Availability and permissions vary |
Use the smallest directory category that matches the data. A cached image does not belong in documents. A database should usually live in application support or the directory selected by its database package.
Getting a directory
Add path_provider to the dependencies in pubspec.yaml, then run flutter pub get:
dependencies:
flutter:
sdk: flutter
path_provider: ^2.1.5The following helper returns the application support directory and creates a file path inside it:
import 'dart:io';
import 'package:path_provider/path_provider.dart';
Future<File> settingsFile() async {
final directory = await getApplicationSupportDirectory();
return File('${directory.path}/settings.json');
}
Future<void> saveText(String value) async {
final file = await settingsFile();
await file.writeAsString(value);
}
Future<String?> loadText() async {
final file = await settingsFile();
if (!await file.exists()) {
return null;
}
return file.readAsString();
}Each directory lookup is asynchronous because the plugin communicates with the host platform. The returned Directory contains a platform-generated path. The File object does not write anything until you call writeAsString, writeAsBytes, or another write method.
Application support is a good default for private files owned by the app. Use application documents when the file represents user data that should be preserved and may be exposed to backup or file-sharing features.
Temporary files and cleanup
Use getTemporaryDirectory() for data that can be recreated:
import 'dart:io';
import 'package:path_provider/path_provider.dart';
Future<File> thumbnailFile(String imageId) async {
final directory = await getTemporaryDirectory();
return File('${directory.path}/thumbnail_$imageId.jpg');
}Do not store authentication tokens or the only copy of user data there. The operating system can remove temporary files when storage is low, when the app is not running, or according to platform-specific cleanup rules.
Avoid building paths from user input without validation. A filename containing path separators could escape the directory you intended. Generate filenames from trusted identifiers, or validate and normalize user-provided names before using them.
How the choice fits the app
The directory decision comes before serialization. Once you select the location, a later layer can encode a model as JSON and write it to a File. A repository can hide both choices from the widget layer.
Exercise
Where should an app store a downloaded image that can always be downloaded again?
Show answer
Store it in the temporary directory, because the image is disposable and the operating system may remove it.