【问题标题】:How to read a json file without assets flutter如何在没有资产抖动的情况下读取 json 文件
【发布时间】:2022-11-03 16:06:12
【问题描述】:

我正在尝试执行一个 json 文件,该文件显示 2 条带有 bat 文件的路由。

要读取文件,我使用 path_provider 来本地化 json 文件,所以我已经完成了这部分。我需要知道为什么程序无法重新调整文本。我将所有信息放在一个列表中,bc 是读取所有信息的正确方法。


dynamic complete_route = '';

_functionX(String args1, String args2) async {
  var shell = Shell();
  try {
    final dir = await getApplicationDocumentsDirectory();
    String d = dir.path;
    final path = d;

    final route = await ('$path\\config.json');

    String contenido = await _leerArchivo(route);
    String local_route = complete_route;

    shell.run('$local_route $args1 $args2');
  } catch (e) {
    debug('error', true);
    debug(e, true);
  }
}

List lista = [];

_leerArchivo(String ruta) async {
  try {
    //final File f = File(ruta);

    final res = await json.decode(ruta);

    lista = res["routes"];

    complete_route = res.toString();

    return lista;
  } catch (e) {
    return e.toString();
  }
}

【问题讨论】:

  • 您到底想从哪里阅读?应用程序运行时的项目还是来自应用程序?
  • 当我运行应用程序时

标签: json flutter file dart


【解决方案1】:

在清单文件中添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

在 <application .... /application> 中放入以下权限

android:requestLegacyExternalStorage="true"

rootBundle 用于访问应用程序的资源,不能用于访问手机存储中的文件。

打开文件

File jsonFile = await File("${dir.path}/demofolder/demo.json");

然后使用解码这个 jsonFile

var jsonData = json.decode(jsonFile.readAsStringSync());

【讨论】:

    猜你喜欢
    • 2022-01-18
    • 2012-03-11
    • 2019-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-02
    • 2017-11-13
    相关资源
    最近更新 更多