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

Viewed 885

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 Answers
  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');
Related