Android FileSystemException:无法打开文件,路径=';assets/busStops.txt';(操作系统错误:没有这样的文件或目录,errno=2)
我似乎无法加载存储在资产中的txt文件。每当我尝试访问它时,都会出现以下错误:Android FileSystemException:无法打开文件,路径=';assets/busStops.txt';(操作系统错误:没有这样的文件或目录,errno=2),android,flutter,dart,Android,Flutter,Dart,我似乎无法加载存储在资产中的txt文件。每当我尝试访问它时,都会出现以下错误: FileSystemException: Cannot open file, path = 'assets/busStops.txt' (OS Error: No such file or directory, errno = 2) 我也已将该文件添加到我的pubspec文件中: assets: - assets/BusSD.png - assets/SubtractBD.png - a
FileSystemException: Cannot open file, path = 'assets/busStops.txt' (OS Error: No such file or directory, errno = 2)
我也已将该文件添加到我的pubspec文件中:
assets:
- assets/BusSD.png
- assets/SubtractBD.png
- assets/VectorDD.png
- assets/busRoutes.txt
- assets/busStops.txt
这是我访问文件的代码
class FileUtilsBusStopInfo {
static Future<String> get getFilePath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
static Future<File> get getFile async {
//final path = await getFilePath;
return File('assets/busStops.txt');
}
static Future<File> saveToFile(String data) async {
final file = await getFile;
return file.writeAsString(data);
}
static Future<String> readFromFile() async {
try {
final file = await getFile;
String fileContents = await file.readAsString();
print(fileContents);
return fileContents;
} catch (e) {
print(e);
print('returning blank');
return "";
}
}
}
类FileUtilsBusStopInfo{
静态未来获取getFilePath异步{
最终目录=等待getApplicationDocumentsDirectory();
返回directory.path;
}
静态未来获取getFile异步{
//最终路径=等待getFilePath;
返回文件('assets/busStops.txt');
}
静态未来保存文件(字符串数据)异步{
最终文件=等待获取文件;
返回文件.writeAsString(数据);
}
静态未来readFromFile()异步{
试一试{
最终文件=等待获取文件;
String fileContents=await file.readAsString();
打印(文件内容);
返回文件内容;
}捕获(e){
印刷品(e);
打印(“返回空白”);
返回“”;
}
}
}
不过,图像加载效果很好。如何解决此问题?提前谢谢你 尝试通过以下方式加载文件:
import'包:flatter/services.dart'显示rootBundle;
//方法
最终数据=rootBundle.load('assets/busStops.txt');
// ...
flatter中的资产是通过其服务加载的,而不是从设备的文件系统加载的。您是否先执行了
flatter pub获取
,然后运行代码?