1、在pubspec.yaml文件中引入依赖 path_provider: ^1.1.0,具体使用版本可到官网查询
2、获取设备路径:
getApplicationDocumentsDirectory() 路径相当于 '/data/user/0/xx.xx.xx/app_flutter'
getTemporaryDirectory() 路径相当于'/data/user/0/xx.xx.xx/cache'
getExternalStorageDirectory() 仅Android平台可用,路径相当于'/storage/emulated/0'外置存储根路径
getApplicationSupportDirectory() 仅Ios平台可用
3、在外置存储上创建自己的目录
final filepath = await getExternalStorageDirectory();
var file = Directory(filepath.path+"/"+"flutter");
try {
bool exists = await file.exists();
if (!exists) {
await file.create();
}
} catch (e) {
print(e);
}
4、创建文件
Future<File> getFile(String path,String fileName) async {
final filePath = path+"/"+fileName;
//或者file对象(操作文件记得导入import 'dart:io')
return new File(filePath);
}