gpt4 book ai didi

dart - 如何在 flutter 中提供路径 - 路径组合

转载 作者:IT王子 更新时间:2023-10-29 06:55:17 24 4
gpt4 key购买 nike

我正在 Flutter 中创建一个测验应用程序,为此我在 CSV 文件中收集了一些问题。我想将 CSV 文件存储在 firebase 中,并通过读取 CSV 文件将问题显示到应用程序中。但是为了检查读取文件是否尽可能简单,我尝试以这种方式读取一个虚拟文件:

new File('file.txt').readAsString().then((String contents) {
print(contents);
});

在返回 Widget 之前从 main.dart 开始。但是我得到这个错误:

`FileSystemException: Cannot open file, path = 'file.txt' (OS Error: No such file or directory, errno = 2)`

即使我在与“main.dart”相同的目录中创建了一个虚拟“file.txt”文件。

我尝试过“./file.txt”,甚至是 Windows 资源管理器中的绝对路径,但似乎都不起作用。

如何解决这个问题?

最佳答案

path_provider 包允许您访问 temp 和 appDir 目录

https://pub.dartlang.org/packages/path_provider

Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;

Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;

您可以使用https://pub.dartlang.org/packages/pathjoin() 方法以依赖于平台的方式连接路径,或者只使用像

这样的字符串连接
String filePath = '${appDocDir.path}/file.txt';

new File(filePath).readAsString().then((String contents) {
print(contents);
});

关于dart - 如何在 flutter 中提供路径 - 路径组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50208485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com