5
5
import 'dart:async' ;
6
6
import 'dart:io' show Directory;
7
7
8
- import 'package:flutter/services.dart' ;
9
- import 'package:meta/meta.dart' ;
10
- import 'package:platform/platform.dart' ;
8
+ import 'package:path_provider_platform_interface/path_provider_platform_interface.dart' ;
11
9
12
- const MethodChannel _channel =
13
- MethodChannel ( 'plugins.flutter.io/path_provider' ) ;
10
+ export 'package:path_provider_platform_interface/path_provider_platform_interface.dart'
11
+ show StorageDirectory ;
14
12
15
- Platform _platform = const LocalPlatform ();
16
-
17
- /// This API is only exposed for the unit tests. It should not be used by
18
- /// any code outside of the plugin itself.
19
- @visibleForTesting
20
- void setMockPathProviderPlatform (Platform platform) {
21
- _platform = platform;
22
- }
13
+ PathProviderPlatform get _platform => PathProviderPlatform .instance;
23
14
24
15
/// Path to the temporary directory on the device that is not backed up and is
25
16
/// suitable for storing caches of downloaded files.
@@ -33,8 +24,7 @@ void setMockPathProviderPlatform(Platform platform) {
33
24
///
34
25
/// On Android, this uses the `getCacheDir` API on the context.
35
26
Future <Directory > getTemporaryDirectory () async {
36
- final String path =
37
- await _channel.invokeMethod <String >('getTemporaryDirectory' );
27
+ final String path = await _platform.getTemporaryPath ();
38
28
if (path == null ) {
39
29
return null ;
40
30
}
@@ -52,8 +42,7 @@ Future<Directory> getTemporaryDirectory() async {
52
42
///
53
43
/// On Android, this function uses the `getFilesDir` API on the context.
54
44
Future <Directory > getApplicationSupportDirectory () async {
55
- final String path =
56
- await _channel.invokeMethod <String >('getApplicationSupportDirectory' );
45
+ final String path = await _platform.getApplicationSupportPath ();
57
46
if (path == null ) {
58
47
return null ;
59
48
}
@@ -67,11 +56,7 @@ Future<Directory> getApplicationSupportDirectory() async {
67
56
/// On Android, this function throws an [UnsupportedError] as no equivalent
68
57
/// path exists.
69
58
Future <Directory > getLibraryDirectory () async {
70
- if (_platform.isAndroid) {
71
- throw UnsupportedError ('Functionality not available on Android' );
72
- }
73
- final String path =
74
- await _channel.invokeMethod <String >('getLibraryDirectory' );
59
+ final String path = await _platform.getLibraryPath ();
75
60
if (path == null ) {
76
61
return null ;
77
62
}
@@ -88,8 +73,7 @@ Future<Directory> getLibraryDirectory() async {
88
73
/// using [getExternalStorageDirectory] instead if data is intended to be visible
89
74
/// to the user.
90
75
Future <Directory > getApplicationDocumentsDirectory () async {
91
- final String path =
92
- await _channel.invokeMethod <String >('getApplicationDocumentsDirectory' );
76
+ final String path = await _platform.getApplicationDocumentsPath ();
93
77
if (path == null ) {
94
78
return null ;
95
79
}
@@ -105,11 +89,7 @@ Future<Directory> getApplicationDocumentsDirectory() async {
105
89
///
106
90
/// On Android this uses the `getExternalFilesDir(null)` .
107
91
Future <Directory > getExternalStorageDirectory () async {
108
- if (_platform.isIOS) {
109
- throw UnsupportedError ('Functionality not available on iOS' );
110
- }
111
- final String path =
112
- await _channel.invokeMethod <String >('getStorageDirectory' );
92
+ final String path = await _platform.getExternalStoragePath ();
113
93
if (path == null ) {
114
94
return null ;
115
95
}
@@ -130,65 +110,11 @@ Future<Directory> getExternalStorageDirectory() async {
130
110
/// On Android this returns Context.getExternalCacheDirs() or
131
111
/// Context.getExternalCacheDir() on API levels below 19.
132
112
Future <List <Directory >> getExternalCacheDirectories () async {
133
- if (_platform.isIOS) {
134
- throw UnsupportedError ('Functionality not available on iOS' );
135
- }
136
- final List <String > paths =
137
- await _channel.invokeListMethod <String >('getExternalCacheDirectories' );
113
+ final List <String > paths = await _platform.getExternalCachePaths ();
138
114
139
115
return paths.map ((String path) => Directory (path)).toList ();
140
116
}
141
117
142
- /// Corresponds to constants defined in Androids `android.os.Environment` class.
143
- ///
144
- /// https://developer.android.com/reference/android/os/Environment.html#fields_1
145
- enum StorageDirectory {
146
- /// Contains audio files that should be treated as music.
147
- ///
148
- /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_MUSIC.
149
- music,
150
-
151
- /// Contains audio files that should be treated as podcasts.
152
- ///
153
- /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_PODCASTS.
154
- podcasts,
155
-
156
- /// Contains audio files that should be treated as ringtones.
157
- ///
158
- /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_RINGTONES.
159
- ringtones,
160
-
161
- /// Contains audio files that should be treated as alarm sounds.
162
- ///
163
- /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_ALARMS.
164
- alarms,
165
-
166
- /// Contains audio files that should be treated as notification sounds.
167
- ///
168
- /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_NOTIFICATIONS.
169
- notifications,
170
-
171
- /// Contains images. See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_PICTURES.
172
- pictures,
173
-
174
- /// Contains movies. See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_MOVIES.
175
- movies,
176
-
177
- /// Contains files of any type that have been downloaded by the user.
178
- ///
179
- /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_DOWNLOADS.
180
- downloads,
181
-
182
- /// Used to hold both pictures and videos when the device filesystem is
183
- /// treated like a camera's.
184
- ///
185
- /// See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_DCIM.
186
- dcim,
187
-
188
- /// Holds user-created documents. See https://developer.android.com/reference/android/os/Environment.html#DIRECTORY_DOCUMENTS.
189
- documents,
190
- }
191
-
192
118
/// Paths to directories where application specific data can be stored.
193
119
/// These paths typically reside on external storage like separate partitions
194
120
/// or SD cards. Phones may have multiple storage directories available.
@@ -206,13 +132,8 @@ Future<List<Directory>> getExternalStorageDirectories({
206
132
/// how this type translates to Android storage directories.
207
133
StorageDirectory type,
208
134
}) async {
209
- if (_platform.isIOS) {
210
- throw UnsupportedError ('Functionality not available on iOS' );
211
- }
212
- final List <String > paths = await _channel.invokeListMethod <String >(
213
- 'getExternalStorageDirectories' ,
214
- < String , dynamic > {'type' : type? .index},
215
- );
135
+ final List <String > paths =
136
+ await _platform.getExternalStoragePaths (type: type);
216
137
217
138
return paths.map ((String path) => Directory (path)).toList ();
218
139
}
@@ -223,14 +144,7 @@ Future<List<Directory>> getExternalStorageDirectories({
223
144
/// On Android and on iOS, this function throws an [UnsupportedError] as no equivalent
224
145
/// path exists.
225
146
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' );
147
+ final String path = await _platform.getDownloadsPath ();
234
148
if (path == null ) {
235
149
return null ;
236
150
}
0 commit comments