// 缓存接口数据
Future<dynamic> writeCache(type, data) async {
// 获取路径
var _filePath = await getFilePath(type);
File _file = new File('$path/appointment');
await _file.writeAsStringSync(data.toString());
return;
}
// 从文件中读取数据
Future<String> readCache(fileName) async {
// 数据缓存文件夹路径
String _filePath = await getFilePath('storage');
try {
File _file = File('$_filePath/$fileName');
// Read the file
String contents = await _file.readAsStringSync();
return contents;
} catch (e) {
print('error: $e');
return '';
}
}
// 获取app临时文件路径
Future getFilePath(type) async{
var filepath = await getApplicationDocumentsDirectory(); // App内部file:data.com.nufang.zao/app-flutter/
// var filepath = await getTemporaryDirectory(); // App内部file:data.com.nufang.zao/ache/
// var filepath = await getExternalStorageDirectory(); // App内部file:data.com.nufang.zao/file/
var file = Directory(filepath.path + '/$type');
try {
bool exists = await file.exists();
if (!exists) {
await file.create();
}
} catch (e) {
print(e);
}
return file.path;
}
|