android - FileSystemException:无法打开文件,路径 = 'assets/busStops.txt'(操作系统错误:没有这样的文件或目录,errno = 2)
问题描述
我似乎无法加载存储在资产中的 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
- 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 "";
}
}
}
图像加载正常。我可以做些什么来解决这个问题?先感谢您!
解决方案
尝试通过以下方式加载文件:
import 'package:flutter/services.dart' show rootBundle;
// Method
final data = rootBundle.load('assets/busStops.txt');
// ...
Flutter 中的资产由其服务加载,而不是从设备的文件系统加载。
推荐阅读
- python - 无法安装 Tensorflow,tensorflow-base-1.12 错误
- java - 从 Android Studio 中的自定义应用程序发送电子邮件 + 附件,为什么 URI 为空?
- d3.js - D3.JS 根据输入变化重新计算 datum() 值
- excel - 将多个excel文件中的数据合并到一个主文件中
- mqtt - 对 Mosquitto MQTT 服务器进行分区
- java - Selenium TestNG 不同维度的相同测试
- amazon-s3 - 无法将 AWS quicksight 连接到 S3 存储桶
- scala - Scala:考虑到除“(”和“)”之外的所有特殊字符都允许使用和不使用拆分的情况下,将字符串转换为字符串数组
- python - 使用python在Windows上检查用户密码
- react-native - LoginManager.logInWithReadPermissions 没有在 react-native-fsdk 中调用回调