From 94f2056609684560c4c2b0e7283eec2eb2fa1625 Mon Sep 17 00:00:00 2001 From: zhoujiaying Date: Sat, 16 Dec 2023 14:22:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5path=5Fprovider=20sig?= =?UTF-8?q?=E4=BB=93=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhoujiaying --- .../path_provider/example/lib/main.dart | 19 ++++++++++--------- .../path_provider_ohos/example/lib/main.dart | 16 ++++++++++++++++ .../lib/path_provider_ohos.dart | 16 ++++++++++++---- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/packages/path_provider/path_provider/example/lib/main.dart b/packages/path_provider/path_provider/example/lib/main.dart index fcf0e138a..ca00f08da 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 21f1e11c1..4f544345a 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 f5d23d218..9917bb858 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(); } } -- Gitee