简体   繁体   中英

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

I need to read simple json file synchronously from assets folder. I have made a function which is doing what I need:

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.

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 :
flutter:
  assets:
    - assets/
    - assets/ble_db/
  1. make the path like this
var config = new File('assets/ble_db/service_uuids.json');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM