ensureProfileLoaded static method

Future ensureProfileLoaded({
  1. String? path,
})

ensureProfileLoaded this method will cache the profile json into data which will speed up the next loop and searching profile

Implementation

static Future ensureProfileLoaded({String? path}) async {
  /// check where this global capabilities is empty then load capabilities.json
  /// else do nothing
  if (printCapabilities.isEmpty == true) {
    final content = await rootBundle.loadString(
        path ?? 'packages/drago_pos_printer/assets/capabilities.json');
    var _capabilities = json.decode(content);
    printCapabilities = Map.from(_capabilities);

    _capabilities['profiles'].forEach((k, v) {
      printProfiles.add({
        'key': k,
        'vendor': v['vendor'] is String ? v['vendor'] : '',
        'model': v['model'] is String ? v['model'] : '',
        'description': v['description'] is String ? v['description'] : '',
      });
    });

    /// assert that the capabilities will be not empty
    assert(printCapabilities.isNotEmpty);
  } else {
    print("capabilities.length is already loaded");
  }
}