1. 准备工作
1.1 添加依赖
在pubspec.yaml文件下添加
dependencies:
path_provider: ^1.6.9
1.2 安装
如果保存了有自动执行则最好,如果没有那么在终端运行flutter packages get命令
1.3 导入
import 'package:path_provider/path_provider.dart';
2. 方法
2.1 获取临时文件路径(IOS和安卓通用)
getTemporaryDirectory().then((value) => print(value));
2.2 获取应用支持目录(IOS和安卓通用)
getApplicationSupportDirectory().then((value) => print(value));
2.3 获取应用文件目录(IOS和安卓通用)
getApplicationDocumentsDirectory().then((value) => print(value));
2.4 获取应用持久存储目录路径(仅IOS可用)
getLibraryDirectory().then((value) => print(value));
2.5 获取外部存储目录(仅安卓可用)
getExternalStorageDirectory().then((value) => print(value));
2.6 获取外部存储目录列表(仅安卓可用)
getExternalStorageDirectories().then((value) => print(value));
2.7 获取外部缓存目录(仅安卓可用)
getExternalCacheDirectories().then((value) => print(value));
2.8 获取下载目录(仅桌面可用 安卓和IOS报错)
getDownloadsDirectory().then((value) => print(value));
插件的内容较少,使用也比较简单,仅仅只是用于获取路径,并没有操作文件和目录的功能,因此,需要搭配Director和File等进行操作。