[英]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?我如何同步从资产文件夹中读取文件?
pubspec.yaml
:pubspec.yaml
中添加了这个:flutter:
assets:
- assets/
- assets/ble_db/
var config = new File('assets/ble_db/service_uuids.json');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.