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

Commit a99812b

Browse files
authored
[path_provider] now supports getDownloadsDirectory() on macOS. (#2436)
Adds support for retrieving a user's downloads directory on macOS using the path_provider plugin.
1 parent ec39e65 commit a99812b

File tree

7 files changed

+41
-2
lines changed

7 files changed

+41
-2
lines changed

packages/path_provider/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.6.0
2+
3+
* Support for retrieving the downloads directory was added.
4+
The call for this is `getDownloadsDirectory`.
5+
16
## 1.5.1
27

38
* Remove the deprecated `author:` field from pubspec.yaml

packages/path_provider/lib/path_provider.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,23 @@ Future<List<Directory>> getExternalStorageDirectories({
216216

217217
return paths.map((String path) => Directory(path)).toList();
218218
}
219+
220+
/// Path to the directory where downloaded files can be stored.
221+
/// This is typically only relevant on desktop operating systems.
222+
///
223+
/// On Android and on iOS, this function throws an [UnsupportedError] as no equivalent
224+
/// path exists.
225+
Future<Directory> getDownloadsDirectory() async {
226+
if (_platform.isAndroid) {
227+
throw UnsupportedError('Functionality not available on Android');
228+
}
229+
if (_platform.isIOS) {
230+
throw UnsupportedError('Functionality not available on iOS');
231+
}
232+
final String path =
233+
await _channel.invokeMethod<String>('getDownloadsDirectory');
234+
if (path == null) {
235+
return null;
236+
}
237+
return Directory(path);
238+
}

packages/path_provider/path_provider_macos/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.3
2+
3+
* Added support for user's downloads directory.
4+
15
## 0.0.2+1
26

37
* Update README.

packages/path_provider/path_provider_macos/macos/Classes/PathProviderPlugin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class PathProviderPlugin: NSObject, FlutterPlugin {
4949
result(path)
5050
case "getLibraryDirectory":
5151
result(getDirectory(ofType: FileManager.SearchPathDirectory.libraryDirectory))
52+
case "getDownloadsDirectory":
53+
result(getDirectory(ofType: FileManager.SearchPathDirectory.downloadsDirectory))
5254
default:
5355
result(FlutterMethodNotImplemented)
5456
}

packages/path_provider/path_provider_macos/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: path_provider_macos
22
description: macOS implementation of the path_provider plugin
3-
version: 0.0.2+1
3+
version: 0.0.3
44
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider/path_provider_macos
55

66
flutter:

packages/path_provider/pubspec.yaml

Lines changed: 1 addition & 1 deletion
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 the Android &
33
iOS file systems, such as the temp and app data directories.
44
homepage: https://github.com/flutter/plugins/tree/master/packages/path_provider
5-
version: 1.5.1
5+
version: 1.6.0
66

77
flutter:
88
plugin:

packages/path_provider/test/path_provider_test.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,12 @@ void main() {
226226
);
227227
expect(directories.map((Directory d) => d.path).toList(), equals(paths));
228228
});
229+
230+
test('DownloadsDirectory path macos test', () async {
231+
setMockPathProviderPlatform(FakePlatform(operatingSystem: 'macos'));
232+
final String fakePath = "/foo/bar/baz";
233+
response = fakePath;
234+
final Directory directory = await getDownloadsDirectory();
235+
expect(directory.path, equals(fakePath));
236+
});
229237
}

0 commit comments

Comments
 (0)