Skip to content

[path_provider] added android support for getDownloadsDirectory #4167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/path_provider/path_provider_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## NEXT
## 2.0.28

* Updates minimum supported SDK version to Flutter 3.3/Dart 2.18.
* Adds support for getDownloadsDirectory in Android

## 2.0.27

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class PathProviderAndroid extends PathProviderPlatform {
}

@override
Future<String?> getDownloadsPath() {
throw UnsupportedError('getDownloadsPath is not supported on Android');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed incorrect throw error

Future<String?> getDownloadsPath() async {
final List<String>? paths =
await getExternalStoragePaths(type: StorageDirectory.downloads);
return paths?.first;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: path_provider_android
description: Android implementation of the path_provider plugin.
repository: https://github.com/flutter/packages/tree/main/packages/path_provider/path_provider_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+path_provider%22
version: 2.0.27
version: 2.0.28

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,9 @@ void main() {
});
} // end of for-loop

test('getDownloadsPath fails', () async {
try {
await pathProvider.getDownloadsPath();
fail('should throw UnsupportedError');
} catch (e) {
expect(e, isUnsupportedError);
}
test('getDownloadsPath succeeds', () async {
final String? path = await pathProvider.getDownloadsPath();
expect(path, kExternalStoragePaths);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two code paths through the new method, success and failure. Each path needs a unit test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've updated the new method to return a nullable path

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You still need a test for the case where it fails (e.g., to catch the fact that your current code will, incorrectly, throw if no external storage paths are returned).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current function now only returns a path, one code path, so I think 1 test is enough? (getDownloadsPath succeed)

Cause it no longer throws an error, hence in test we can't expect it to throw something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current function now only returns a path, one code path, so I think 1 test is enough? (getDownloadsPath succeed)

As my comment above says, there is a bug in your current implementation, which is compelling evidence that the current test is insufficient.

Cause it no longer throws an error

Please test your code with an empty return array if you don't believe me that your code can still throw.

});
});
}