简体   繁体   English

如何使用 flutter 从资产文件夹同步读取文件(无需等待)

[英]How to read file from assets folder synchronously (without await) using flutter

I need to read simple json file synchronously from assets folder.我需要从 assets 文件夹中同步读取简单的 json 文件。 I have made a function which is doing what I need:我做了一个 function 正在做我需要的事情:

Future<void> init() async {
    _charList = jsonDecode(await rootBundle.loadString('assets/ble_db/characteristic_uuids.json'));
    _servicesList = jsonDecode(await rootBundle.loadString('assets/ble_db/service_uuids.json'));        
  }

But the only problem of this aproach is async behaviour.但是这种方法的唯一问题是异步行为。 For my program it would be much better if it would be a blocking code which i could call from normal function.对于我的程序,如果它是我可以从普通 function 调用的阻塞代码会更好。

When I am trying to use this approach:当我尝试使用这种方法时:

var config = new File('./assets/ble_db/service_uuids.json');
var str = config.readAsStringSync();

I am getting an error我收到一个错误

Cannot open file, path = './assets/ble_db/service_uuids.json' (OS Error: No such file or directory, errno = 2)

How could i read file from assets folder synchronously?我如何同步从资产文件夹中读取文件?

  1. did you add this in your pubspec.yaml :你在你的pubspec.yaml中添加了这个:
flutter:
  assets:
    - assets/
    - assets/ble_db/
  1. make the path like this使路径像这样
var config = new File('assets/ble_db/service_uuids.json');

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

相关问题 如何在颤振应用程序的资产文件夹中写入和读取文件? - How to write to and read from a file in my assets folder in a flutter app? 如何使用 flutter 读取没有资产的 json 文件 - How to read a json file without assets with flutter 如何从资产文件夹打开Excel文件 - how to open an excel file from assets folder 如何将本地文件保存到 Flutter 的 assets 文件夹中 - How can I save a local file into assets folder in Flutter 使用资产从android中的文件读取 - read from file in android using assets 如何从素材文件夹中读取要复制到Android模拟器或设备中的大型Sqlite文件? - How to read LARGE Sqlite file to be copied into Android emulator, or device from assets folder? Angular:如何读取位于 assets 文件夹中的文本文件? - Angular: How do I read a text file located in the assets folder? 如何从片段中读取文本文件? - how to read text file from assets in fragment? 如何使用C#从zip文件夹中读取文件而不在asp.net 2中解压缩 - How to read file from zip folder without extracting in asp.net 2 using C# 在 Flutter 中,将大文件从 Assets 文件夹复制到 Application Documents 文件夹 - Inside Flutter, copy large file from Assets Folder to Application Documents Folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM