【发布时间】:2019-12-21 14:28:49
【问题描述】:
我开始学习 Flutter,一切正常,但它不读取 xml 文件 这是 pubspec.yaml 部分:
flutter:
uses-material-design: true
assets:
- assets/
- assets/xml/strings.xml
这是应该读取 xml 的部分:
void main(){
String file = "";
switch (type){
case TYPE_STRING:
file = 'assets/xml/strings.xml';
break;
}
readFileAsync(file);
}
void readFileAsync(String filePath) {
File file = new File(filePath);
Future<String> futureContent = file.readAsString();
futureContent.then((xmlString) => {
parseXml(xmlString)
});
}
我得到的错误是
Unhandled Exception: FileSystemException: Cannot open file, path = 'assets/xml/strings.xml' (OS Error: No such file or directory, errno = 2)
所以,在我看来,我设置的一切都正确,有什么问题?
【问题讨论】:
-
相对路径要求当前工作目录按照您的预期设置。除非您明确要求 .it,否则很少会出现这种情况。尝试打印出
file.getAbsolutePath()并查看 Java 当前的位置。 -
使用
rootBundle- 这就是你应该如何访问你的assets文件夹 - 文档说:“加载此应用程序的AssetBundle。rootBundle包含打包的资源在构建应用程序时与应用程序一起使用。要将资源添加到应用程序的 rootBundle,请将它们添加到应用程序的 pubspec.yaml 清单的颤振部分的 assets 子部分。"