Flutter找不到txt文件

发布于 2025-01-21 04:03:26 字数 782 浏览 0 评论 0原文

我正在尝试使用 ini package 以读取文件,但是当我尝试尝试时要仅通过阅读其内容访问该文件,我会收到以下错误:没有此类文件或目录。

这是错误的代码:

class _FactsListScreenState extends State<FactsListScreen> {
  @override
  Widget build(BuildContext context) {
    File file = File("Assets/Facts/English.txt");
    Config config = Config.fromStrings(file.readAsLinesSync());
    print(config.sections());

有趣,但是使用rootbundle.loadstring(“资产/facts/English.txt”)它可以使用,但是当使用file(“资产/事实/英语)时。

是的,我已经在pubspec.yaml中包含了文件夹:

​。

​为什么找不到文件?

I'm trying to use the INI Package to read a file, but when I'm trying to access the file just by reading its contents I get the following error: No such file or directory.

Here is the faulty code:

class _FactsListScreenState extends State<FactsListScreen> {
  @override
  Widget build(BuildContext context) {
    File file = File("Assets/Facts/English.txt");
    Config config = Config.fromStrings(file.readAsLinesSync());
    print(config.sections());

Interesting, but when using rootBundle.loadString("Assets/Facts/English.txt") it works, but not when using File("Assets/Facts/English.txt")

I also need to mention that I'm running the app on an Android simulator, if that makes any difference.

Yes, I have included the folder in pubspec.yaml:

pubspec.yaml screenshot

Here is my file structure to show that the file is actually there:

File Structure

What's the problem with the code? Why it can't find the file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花易冷人易散 2025-01-28 04:03:27

您无法使用 File 从资产中读取文件,因为 File 是对文件系统上文件的引用。您无法通过文件访问资产文件。
每当您尝试调用时,

File file = File("Assets/Facts/English.txt");

它都会尝试从设备而不是应用程序的资产文件夹中读取文件。

您可以使用以下函数从资产创建文件。

Future<File> getImageFileFromAssets(String path) async {
  final byteData = await rootBundle.load('assets/$path');

   final file = File('${(await getTemporaryDirectory()).path}/$path');
    await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

  return file;
}

You can't use File to read files from assets due to File is a reference to a file on the file system. You can't access assets files by File.
Whenever you tried calling like

File file = File("Assets/Facts/English.txt");

it will try to read files from a device not from the app's assets folder.

You could use the below function to make file from assets.

Future<File> getImageFileFromAssets(String path) async {
  final byteData = await rootBundle.load('assets/$path');

   final file = File('${(await getTemporaryDirectory()).path}/$path');
    await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

  return file;
}
美煞众生 2025-01-28 04:03:27

您可以将您的PubSpec文件更改为:

资产/

,然后致电:

file file = file(“ facts/English.txt”);

You can change your pubspec file to only:

Assets/

And then call:

File file = File("Facts/English.txt");

别念他 2025-01-28 04:03:27

将您的Asset目录重命名为assets(小写)。因为根据 flutter 官方文档(使用 lowercase_with_underscores.fileextension 命名库、包、目录和源文件)检查此 链接供参考。

因此,在您的情况下,请执行以下操作:

1. Rename your Asset directory to assets 
2. Rename subdirectories to audio, facts
3. also rename file to english.txt 

使用字符串文件路径,如瘦 'assets/facts/english.txt'

Rename your Asset directory to assets (small case). because as per the flutter official document (name libraries, packages, directories, and source files using lowercase_with_underscores.fileextension)check this link for reference.

so in your case do following things:

1. Rename your Asset directory to assets 
2. Rename subdirectories to audio, facts
3. also rename file to english.txt 

use string file path like thin 'assets/facts/english.txt'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文