diff --git a/packages/path_provider/path_provider/example/lib/main.dart b/packages/path_provider/path_provider/example/lib/main.dart index fcf0e138a65e8bf584e01bbf25f4dbdc966028ef..ca00f08da75c3918415f1b25681d3b358b140ed9 100644 --- a/packages/path_provider/path_provider/example/lib/main.dart +++ b/packages/path_provider/path_provider/example/lib/main.dart @@ -198,10 +198,11 @@ class _MyHomePageState extends State { Padding( padding: const EdgeInsets.all(16.0), child: ElevatedButton( - onPressed: - Platform.isAndroid ? null : _requestAppLibraryDirectory, + onPressed: (Platform.isAndroid || Platform.isOhos) + ? null + : _requestAppLibraryDirectory, child: Text( - Platform.isAndroid + (Platform.isAndroid || Platform.isOhos) ? 'Application Library Directory unavailable' : 'Get Application Library Directory', ), @@ -235,11 +236,11 @@ class _MyHomePageState extends State { Padding( padding: const EdgeInsets.all(16.0), child: ElevatedButton( - onPressed: !Platform.isAndroid + onPressed: !(Platform.isAndroid || Platform.isOhos) ? null : _requestExternalStorageDirectory, child: Text( - !Platform.isAndroid + !(Platform.isAndroid || Platform.isOhos) ? 'External storage is unavailable' : 'Get External Storage Directory', ), @@ -256,7 +257,7 @@ class _MyHomePageState extends State { Padding( padding: const EdgeInsets.all(16.0), child: ElevatedButton( - onPressed: !Platform.isAndroid + onPressed: !(Platform.isAndroid || Platform.isOhos) ? null : () { _requestExternalStorageDirectories( @@ -264,7 +265,7 @@ class _MyHomePageState extends State { ); }, child: Text( - !Platform.isAndroid + !(Platform.isAndroid || Platform.isOhos) ? 'External directories are unavailable' : 'Get External Storage Directories', ), @@ -281,11 +282,11 @@ class _MyHomePageState extends State { Padding( padding: const EdgeInsets.all(16.0), child: ElevatedButton( - onPressed: !Platform.isAndroid + onPressed: !(Platform.isAndroid || Platform.isOhos) ? null : _requestExternalCacheDirectories, child: Text( - !Platform.isAndroid + !(Platform.isAndroid || Platform.isOhos) ? 'External directories are unavailable' : 'Get External Cache Directories', ), diff --git a/packages/path_provider/path_provider_ohos/example/lib/main.dart b/packages/path_provider/path_provider_ohos/example/lib/main.dart index 21f1e11c13d632f72bd6b8b11b70f88e37c60635..4f544345abc3d94d1bf0ff9ebf59f34de0976f72 100644 --- a/packages/path_provider/path_provider_ohos/example/lib/main.dart +++ b/packages/path_provider/path_provider_ohos/example/lib/main.dart @@ -54,6 +54,7 @@ class _MyHomePageState extends State { Future? _externalDocumentsDirectory; Future?>? _externalStorageDirectories; Future?>? _externalCacheDirectories; + Future? _downloadsDirectory; void _requestTempDirectory() { setState(() { @@ -129,6 +130,12 @@ class _MyHomePageState extends State { }); } + void _requestDownloadsDirectory() { + setState(() { + _downloadsDirectory = provider.getDownloadsPath(); + }); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -210,6 +217,15 @@ class _MyHomePageState extends State { ]), FutureBuilder?>( future: _externalCacheDirectories, builder: _buildDirectories), + Padding( + padding: const EdgeInsets.all(16.0), + child: ElevatedButton( + onPressed: _requestDownloadsDirectory, + child: const Text('Get Downloads Directory'), + ), + ), + FutureBuilder( + future: _downloadsDirectory, builder: _buildDirectory), ], ), ), diff --git a/packages/path_provider/path_provider_ohos/lib/path_provider_ohos.dart b/packages/path_provider/path_provider_ohos/lib/path_provider_ohos.dart index f5d23d218f91b464269033f2e41cd097c93a9a19..9917bb8585bc1e5be154424c516ba66466347814 100644 --- a/packages/path_provider/path_provider_ohos/lib/path_provider_ohos.dart +++ b/packages/path_provider/path_provider_ohos/lib/path_provider_ohos.dart @@ -92,12 +92,20 @@ class PathProviderOhos extends PathProviderPlatform { Future?> getExternalStoragePaths({ StorageDirectory? type, }) async { - return (await _api.getExternalStoragePaths(_convertStorageDirectory(type))) - .cast(); + return _getExternalStoragePaths(type: type); } @override - Future getDownloadsPath() { - throw UnsupportedError('getDownloadsPath is not supported on OHOS'); + Future getDownloadsPath() async { + final List paths = + await _getExternalStoragePaths(type: StorageDirectory.downloads); + return paths.isEmpty ? null : paths.first; + } + + Future> _getExternalStoragePaths({ + StorageDirectory? type, + }) async { + return (await _api.getExternalStoragePaths(_convertStorageDirectory(type))) + .cast(); } }