1.添加path_provider
官方链接见https://pub-web.flutter-io.cn/packages/path_provider
支持平台Android/iOS/Linux/macOS/Windows
2.pubspec.yaml添加依赖
3.示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import 'package:path_provider/path_provider.dart';
class FileMgr {
FileMgr._privateConstructor();
static final FileMgr instance = FileMgr._privateConstructor();
//文档目录
String? _documentsDirectory;
//临时目录
String? _temporaryDirectory;
//
String? _cacheDirectory;
//获取文档目录
Future<String> getDocumentsDirectory() async {
_documentsDirectory ??= (await getApplicationDocumentsDirectory()).path;
return _documentsDirectory!;
}
//获取临时目录
/// - `NSCachesDirectory` on iOS and macOS.
/// - `Context.getCacheDir` on Android.
Future<String> getTemporaryDirectoryPath() async {
_temporaryDirectory ??= (await getTemporaryDirectory()).path;
return _temporaryDirectory!;
}
//获取缓存目录
Future<String> getApplicationSupportDirectoryPath() async {
_cacheDirectory ??= (await getApplicationSupportDirectory()).path;
return _cacheDirectory!;
}
}
|
调用
1
2
|
debugPrint('cacheDirectory:${await FileMgr.instance.getApplicationSupportDirectoryPath()}\n documentsDirectory:${await FileMgr.instance.getDocumentsDirectory()} \n temporaryDirectoryPath:${await FileMgr.instance.getTemporaryDirectoryPath()}');
|
4.效果
1
2
3
|
flutter: cacheDirectory:/Users/ksnowlv/Library/Developer/CoreSimulator/Devices/F35779E0-8AA1-4124-BECC-E1945A8E4000/data/Containers/Data/Application/DD4D8EF3-1CC4-4B8D-9C8F-DF0726E2C55E/Library/Application Support
flutter: documentsDirectory:/Users/ksnowlv/Library/Developer/CoreSimulator/Devices/F35779E0-8AA1-4124-BECC-E1945A8E4000/data/Containers/Data/Application/DD4D8EF3-1CC4-4B8D-9C8F-DF0726E2C55E/Documents
flutter: temporaryDirectoryPath:/Users/ksnowlv/Library/Developer/CoreSimulator/Devices/F35779E0-8AA1-4124-BECC-E1945A8E4000/data/Containers/Data/Application/DD4D8EF3-1CC4-4B8D-9C8F-DF0726E2C55E/Library/Caches
|
5.小结
1.目录
文档目录、临时目录,缓存目录为应用中常用目录,上述仅做演示;不同的平台可能会有差异,注意区别。
文章作者
梵梵爸
上次更新
2024-04-07
许可协议
原创文章,如需转载请注明文章作者和出处。谢谢