Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit d348d44

Browse files
[path_provider] Switch to path_provider_foundation (#6989)
* [path_provider] Switch to `path_provider_foundation` Switches to using the new combined `path_provider_foundation` for iOS and macOS. Also updates the code documentation to make it less Android and iOS specific. The original goal was to make the documentation for the download directory not be actively wrong for the new implementation, but it seemed like a good time to fix 76427 more generally. (The fact that the docs are kind of a mess because the API itself is kind of a mess is now flutter/flutter#118712.) Fixes flutter/flutter#117941 Fixes flutter/flutter#76427 * Remove exclusion * Update test expectations and README * Update test expectations again, and update docs
1 parent c4684bb commit d348d44

File tree

6 files changed

+67
-66
lines changed

6 files changed

+67
-66
lines changed

packages/path_provider/path_provider/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## NEXT
1+
## 2.0.12
22

3+
* Switches to the new `path_provider_foundation` implementation package
4+
for iOS and macOS.
35
* Updates code for `no_leading_underscores_for_local_identifiers` lint.
46
* Updates minimum Flutter version to 2.10.
57
* Fixes avoid_redundant_argument_values lint warnings and minor typos.

packages/path_provider/path_provider/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Directories support by platform:
3636
| External Storage | ✔️ ||| ❌️ | ❌️ |
3737
| External Cache Directories | ✔️ ||| ❌️ | ❌️ |
3838
| External Storage Directories | ✔️ ||| ❌️ | ❌️ |
39-
| Downloads || | ✔️ | ✔️ | ✔️ |
39+
| Downloads || ✔️ | ✔️ | ✔️ | ✔️ |
4040

4141
## Testing
4242

packages/path_provider/path_provider/example/integration_test/path_provider_test.dart

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,16 @@ void main() {
8787
}
8888

8989
testWidgets('getDownloadsDirectory', (WidgetTester tester) async {
90-
if (Platform.isIOS || Platform.isAndroid) {
90+
if (Platform.isAndroid) {
9191
final Future<Directory?> result = getDownloadsDirectory();
9292
expect(result, throwsA(isInstanceOf<UnsupportedError>()));
9393
} else {
9494
final Directory? result = await getDownloadsDirectory();
95-
if (Platform.isMacOS) {
96-
// On recent versions of macOS, actually using the downloads directory
97-
// requires a user prompt, so will fail on CI. Instead, just check that
98-
// it returned a path with the expected directory name.
99-
expect(result?.path, endsWith('Downloads'));
100-
} else {
101-
_verifySampleFile(result, 'downloads');
102-
}
95+
// On recent versions of macOS, actually using the downloads directory
96+
// requires a user prompt (so will fail on CI), and on some platforms the
97+
// directory may not exist. Instead of verifying that it exists, just
98+
// check that it returned a path.
99+
expect(result?.path, isNotEmpty);
103100
}
104101
});
105102
}

packages/path_provider/path_provider/lib/path_provider.dart

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ PathProviderPlatform get _platform => PathProviderPlatform.instance;
4545
/// (and cleaning up) files or directories within this directory. This
4646
/// directory is scoped to the calling application.
4747
///
48-
/// On iOS, this uses the `NSCachesDirectory` API.
48+
/// Example implementations:
49+
/// - `NSCachesDirectory` on iOS and macOS.
50+
/// - `Context.getCacheDir` on Android.
4951
///
50-
/// On Android, this uses the `getCacheDir` API on the context.
51-
///
52-
/// Throws a `MissingPlatformDirectoryException` if the system is unable to
52+
/// Throws a [MissingPlatformDirectoryException] if the system is unable to
5353
/// provide the directory.
5454
Future<Directory> getTemporaryDirectory() async {
5555
final String? path = await _platform.getTemporaryPath();
@@ -63,15 +63,16 @@ Future<Directory> getTemporaryDirectory() async {
6363
/// Path to a directory where the application may place application support
6464
/// files.
6565
///
66+
/// If this directory does not exist, it is created automatically.
67+
///
6668
/// Use this for files you don’t want exposed to the user. Your app should not
6769
/// use this directory for user data files.
6870
///
69-
/// On iOS, this uses the `NSApplicationSupportDirectory` API.
70-
/// If this directory does not exist, it is created automatically.
71-
///
72-
/// On Android, this function uses the `getFilesDir` API on the context.
71+
/// Example implementations:
72+
/// - `NSApplicationSupportDirectory` on iOS and macOS.
73+
/// - The Flutter engine's `PathUtils.getFilesDir` API on Android.
7374
///
74-
/// Throws a `MissingPlatformDirectoryException` if the system is unable to
75+
/// Throws a [MissingPlatformDirectoryException] if the system is unable to
7576
/// provide the directory.
7677
Future<Directory> getApplicationSupportDirectory() async {
7778
final String? path = await _platform.getApplicationSupportPath();
@@ -86,10 +87,14 @@ Future<Directory> getApplicationSupportDirectory() async {
8687
/// Path to the directory where application can store files that are persistent,
8788
/// backed up, and not visible to the user, such as sqlite.db.
8889
///
89-
/// On Android, this function throws an [UnsupportedError] as no equivalent
90-
/// path exists.
90+
/// Example implementations:
91+
/// - `NSApplicationSupportDirectory` on iOS and macOS.
92+
///
93+
/// Throws an [UnsupportedError] if this is not supported on the current
94+
/// platform. For example, this is unlikely to ever be supported on Android,
95+
/// as no equivalent path exists.
9196
///
92-
/// Throws a `MissingPlatformDirectoryException` if the system is unable to
97+
/// Throws a [MissingPlatformDirectoryException] if the system is unable to
9398
/// provide the directory on a supported platform.
9499
Future<Directory> getLibraryDirectory() async {
95100
final String? path = await _platform.getLibraryPath();
@@ -102,14 +107,14 @@ Future<Directory> getLibraryDirectory() async {
102107
/// Path to a directory where the application may place data that is
103108
/// user-generated, or that cannot otherwise be recreated by your application.
104109
///
105-
/// On iOS, this uses the `NSDocumentDirectory` API. Consider using
106-
/// [getApplicationSupportDirectory] instead if the data is not user-generated.
110+
/// Consider using another path, such as [getApplicationSupportDirectory] or
111+
/// [getExternalStorageDirectory], if the data is not user-generated.
107112
///
108-
/// On Android, this uses the `getDataDirectory` API on the context. Consider
109-
/// using [getExternalStorageDirectory] instead if data is intended to be visible
110-
/// to the user.
113+
/// Example implementations:
114+
/// - `NSDocumentDirectory` on iOS and macOS.
115+
/// - The Flutter engine's `PathUtils.getDataDirectory` API on Android.
111116
///
112-
/// Throws a `MissingPlatformDirectoryException` if the system is unable to
117+
/// Throws a [MissingPlatformDirectoryException] if the system is unable to
113118
/// provide the directory.
114119
Future<Directory> getApplicationDocumentsDirectory() async {
115120
final String? path = await _platform.getApplicationDocumentsPath();
@@ -121,13 +126,13 @@ Future<Directory> getApplicationDocumentsDirectory() async {
121126
}
122127

123128
/// Path to a directory where the application may access top level storage.
124-
/// The current operating system should be determined before issuing this
125-
/// function call, as this functionality is only available on Android.
126129
///
127-
/// On iOS, this function throws an [UnsupportedError] as it is not possible
128-
/// to access outside the app's sandbox.
130+
/// Example implementation:
131+
/// - `getExternalFilesDir(null)` on Android.
129132
///
130-
/// On Android this uses the `getExternalFilesDir(null)`.
133+
/// Throws an [UnsupportedError] if this is not supported on the current
134+
/// platform (for example, on iOS where it is not possible to access outside
135+
/// the app's sandbox).
131136
Future<Directory?> getExternalStorageDirectory() async {
132137
final String? path = await _platform.getExternalStoragePath();
133138
if (path == null) {
@@ -136,19 +141,19 @@ Future<Directory?> getExternalStorageDirectory() async {
136141
return Directory(path);
137142
}
138143

139-
/// Paths to directories where application specific external cache data can be
140-
/// stored. These paths typically reside on external storage like separate
141-
/// partitions or SD cards. Phones may have multiple storage directories
142-
/// available.
144+
/// Paths to directories where application specific cache data can be stored
145+
/// externally.
143146
///
144-
/// The current operating system should be determined before issuing this
145-
/// function call, as this functionality is only available on Android.
147+
/// These paths typically reside on external storage like separate partitions
148+
/// or SD cards. Phones may have multiple storage directories available.
146149
///
147-
/// On iOS, this function throws an UnsupportedError as it is not possible
148-
/// to access outside the app's sandbox.
150+
/// Example implementation:
151+
/// - Context.getExternalCacheDirs() on Android (or
152+
/// Context.getExternalCacheDir() on API levels below 19).
149153
///
150-
/// On Android this returns Context.getExternalCacheDirs() or
151-
/// Context.getExternalCacheDir() on API levels below 19.
154+
/// Throws an [UnsupportedError] if this is not supported on the current
155+
/// platform. This is unlikely to ever be supported on any platform other than
156+
/// Android.
152157
Future<List<Directory>?> getExternalCacheDirectories() async {
153158
final List<String>? paths = await _platform.getExternalCachePaths();
154159
if (paths == null) {
@@ -158,18 +163,19 @@ Future<List<Directory>?> getExternalCacheDirectories() async {
158163
return paths.map((String path) => Directory(path)).toList();
159164
}
160165

161-
/// Paths to directories where application specific data can be stored.
166+
/// Paths to directories where application specific data can be stored
167+
/// externally.
168+
///
162169
/// These paths typically reside on external storage like separate partitions
163170
/// or SD cards. Phones may have multiple storage directories available.
164171
///
165-
/// The current operating system should be determined before issuing this
166-
/// function call, as this functionality is only available on Android.
172+
/// Example implementation:
173+
/// - Context.getExternalFilesDirs(type) on Android (or
174+
/// Context.getExternalFilesDir(type) on API levels below 19).
167175
///
168-
/// On iOS, this function throws an UnsupportedError as it is not possible
169-
/// to access outside the app's sandbox.
170-
///
171-
/// On Android this returns Context.getExternalFilesDirs(String type) or
172-
/// Context.getExternalFilesDir(String type) on API levels below 19.
176+
/// Throws an [UnsupportedError] if this is not supported on the current
177+
/// platform. This is unlikely to ever be supported on any platform other than
178+
/// Android.
173179
Future<List<Directory>?> getExternalStorageDirectories({
174180
/// Optional parameter. See [StorageDirectory] for more informations on
175181
/// how this type translates to Android storage directories.
@@ -185,10 +191,12 @@ Future<List<Directory>?> getExternalStorageDirectories({
185191
}
186192

187193
/// Path to the directory where downloaded files can be stored.
188-
/// This is typically only relevant on desktop operating systems.
189194
///
190-
/// On Android and on iOS, this function throws an [UnsupportedError] as no equivalent
191-
/// path exists.
195+
/// The returned directory is not guaranteed to exist, so clients should verify
196+
/// that it does before using it, and potentially create it if necessary.
197+
///
198+
/// Throws an [UnsupportedError] if this is not supported on the current
199+
/// platform.
192200
Future<Directory?> getDownloadsDirectory() async {
193201
final String? path = await _platform.getDownloadsPath();
194202
if (path == null) {

packages/path_provider/path_provider/pubspec.yaml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: path_provider
22
description: Flutter plugin for getting commonly used locations on host platform file systems, such as the temp and app data directories.
33
repository: https://github.com/flutter/plugins/tree/main/packages/path_provider/path_provider
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
5-
version: 2.0.11
5+
version: 2.0.12
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"
@@ -14,21 +14,20 @@ flutter:
1414
android:
1515
default_package: path_provider_android
1616
ios:
17-
default_package: path_provider_ios
18-
macos:
19-
default_package: path_provider_macos
17+
default_package: path_provider_foundation
2018
linux:
2119
default_package: path_provider_linux
20+
macos:
21+
default_package: path_provider_foundation
2222
windows:
2323
default_package: path_provider_windows
2424

2525
dependencies:
2626
flutter:
2727
sdk: flutter
2828
path_provider_android: ^2.0.6
29-
path_provider_ios: ^2.0.6
29+
path_provider_foundation: ^2.1.0
3030
path_provider_linux: ^2.0.1
31-
path_provider_macos: ^2.0.0
3231
path_provider_platform_interface: ^2.0.0
3332
path_provider_windows: ^2.0.2
3433

script/configs/exclude_all_plugins_app.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,3 @@
88

99
# This is a permament entry, as it should never be a direct app dependency.
1010
- plugin_platform_interface
11-
# Temporarily excluded to avoid runtime conflicts with
12-
# path_provider_macos and _ios, which are still what path_provider
13-
# uses. This will be removed when the switch to path_provider_foundation
14-
# is complete. See https://github.com/flutter/flutter/issues/117941
15-
- path_provider_foundation

0 commit comments

Comments
 (0)