ensureProfileLoaded static method

Future<void> 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<void> ensureProfileLoaded({String? path}) async {
  /// check where this global capabilities is empty
  /// then load capabilities.json,
  /// else do nothing
  if (__printCapabilities.isEmpty == true) {
    final String content = await rootBundle.loadString(
      path ?? 'packages/esc_pos_gen/resources/capabilities.json',
    );
    final dynamic _capabilities = json.decode(content);
    __printCapabilities = Map<String, dynamic>.from(
      _capabilities as Map<dynamic, dynamic>,
    );

    _capabilities['profiles'].forEach((dynamic k, dynamic v) {
      __printProfiles.add(<String, dynamic>{
        'key': k,
        'vendor': v['vendor'] is String ? v['vendor'] : '',
        'name': v['name'] is String ? v['name'] : '',
        'description': v['description'] is String ? v['description'] : '',
      });
    });

    /// assert that the capabilities will be not empty
    assert(__printCapabilities.isNotEmpty);
  } else {
    assert(() {
      // ignore: avoid_print
      print('capabilities.length is already loaded');
      return true;
      // ignore: require_trailing_commas
    }());
  }
}