1、本地文件
void readFileDemo(){
_readFileContent().then((value){
print("value:$value");
_writeFileContent("value:$value 12345").then((value){
_readFileContent().then((value){
print(value);
});
});
});
}
///本地文件相关
Future<File> _localFile()async{
///获取document的路径的文件
final directory = await getApplicationDocumentsDirectory();
final path = directory.path;
return File('$path/file.txt');
}
//文件写入
Future<File> _writeFileContent(contents) async{
final file = await _localFile();
return file.writeAsString(contents);
}
///文件读取
Future<String> _readFileContent()async{
try{
File file = await _localFile();
String contents = await file.readAsString();//文件读取
return contents;
}catch(e){
return "";
}
}
2、资源文件读取
import 'package:flutter/services.dart' show rootBundle;
Future<String> loadAssest() async {
return await rootBundle.loadString("");
}
网友评论