简体   繁体   English

无法在所有平台上读取 Flutter 中的文件,该文件在 linux 桌面上可以看到但 web(chrome) 看不到它,android 也看不到

[英]Can't read files in Flutter on all platforms, the file can be seen on linux desktop but web(chrome) does not see it, nor does android

import 'dart:io';
import 'package:path/path.dart';

Future<Map?> readFile(lang) async {
  var pathToFile = join(dirname(Platform.script.toFilePath()), 'text', '$lang.toml');
  var document = File(pathToFile);
  return document;
}

So I have a text directory in my flutter root directory which contains some TOML files, I have no trouble accessing these files on linux but as soon as I run this on android I get a (OS Error: No such file or directory, errno = 2) , and same on web but without the error message, it just returns null, I have also tried this as well:所以我的 flutter 根目录中有一个文本目录,其中包含一些 TOML 文件,我在 linux 上访问这些文件没有问题,但是一旦我在 android 上运行它,我就会得到一个(OS Error: No such file or directory, errno = 2) , 和 web 上一样,但没有错误消息,它只返回 null,我也试过这个:

var document = File('text/$lang.toml');

once again, same result.再一次,同样的结果。 I have seen people use the path_provider package for this but it didn't work anyway, and I would also like to have web compatibility.我已经看到人们为此使用 path_provider 包,但无论如何它都不起作用,而且我还希望具有网络兼容性。 Anyone know how to solve this?有谁知道如何解决这个问题? TY

When using shipped assets, you shouldn't use the host's file system.使用附带的资产时,不应使用主机的文件系统。 Instead use the rootBundle from flutter/services .而是使用flutter/servicesrootBundle

import 'package:flutter/services.dart';

...

Future<String> readFile(String lang) {
  return rootBundle.loadString('text/$lang.toml');
}

Assuming the file is in a text/ directory at the root of your project and it is defined in the pubspec.yaml :假设文件位于项目根目录的text/目录中,并且它在pubspec.yaml定义:

flutter:
  assets:
    - text/

The fact that it worked on Linux is kind of a coincidence, since you're running and developing on the same machine.它在 Linux 上运行的事实有点巧合,因为您在同一台机器上运行和开发。 This solution should work independent of which platform you're targeting.此解决方案应该独立于您的目标平台而工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果文件不存在,则所有下一个文件都不会在Java for Android中读取 - If a file does not exist all of the next files will not be read in Java for Android Android Studio和Favourites-无法查看所有文件 - Android studio and flavours - can not see all files 为什么chrome无法读取android上的本地html文件? - Why can't chrome read local html file on android? android看不到网络服务 - android can't see web service 在 Android 中,多部分实体是否也用于下载文件,因为我看不到 class 的任何示例或详细说明 - In Android Does multipart entity is used for download file too as I can't see any example or details description for that class 无法在Cordova 3.5.0中添加android平台 - Can't add android platforms in cordova 3.5.0 我想创建支持 web、桌面和移动 ui 等所有平台的 flutter 架构 - I want to create flutter architecture that support all platforms like web ,desktop and mobile ui 我看不到 Flutter 中 OutlineInputBorder 中写入的所有文本 - I can't see all texts written inside OutlineInputBorder in Flutter KivyMD,在 android 中看不到图标,但在 linux 上有效 - KivyMD, can't see icons in android but works on linux 我们如何在 flutter 中打开/启动下载文件夹。 正如所有 web 浏览器在下载完成时所做的那样 - how can we open / launch download folder in flutter . As all web browser does when downloading is completed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM