parse method

GS1Barcode parse(
  1. String data, {
  2. CodeType? codeType,
  3. bool ignoreUnknownAIs = false,
})

Parse barcode string

Implementation

GS1Barcode parse(
  String data, {
  CodeType? codeType,
  bool ignoreUnknownAIs = false,
}) {
  if (data.isEmpty) {
    throw GS1DataException(message: 'Barcode is empty');
  }

  // Detect GS1 Digital Link URL (starts with http/https)
  if (data.startsWith('http://') || data.startsWith('https://')) {
    return _parseDigitalLink(data, codeType);
  }

  final codeWithRest = _codeParser(_normalize(data, codeType: codeType));
  if (codeWithRest.code.type == CodeType.UNDEFINED &&
      !_config.allowEmptyPrefix) {
    throw GS1DataException(message: 'FNC1 prefix not found');
  }

  String restOfBarcode = codeWithRest.rest;

  if (restOfBarcode.isEmpty) {
    throw GS1DataException(message: 'No date present');
  }

  final elements = <String, GS1ParsedElement>{};

  while (restOfBarcode.isNotEmpty) {
    final res = _identifyAI(
      restOfBarcode,
      ignoreUnknownAIs,
      _config.customAIs,
    );
    if (res == null) {
      final int nextSeparator = restOfBarcode.indexOf(
        _config.groupSeparator,
        1,
      );
      if (nextSeparator == -1) {
        return GS1Barcode(code: codeWithRest.code, elements: elements);
      } else {
        restOfBarcode = restOfBarcode.substring(nextSeparator + 1);
        continue;
      }
    }
    elements.putIfAbsent(res.element.aiCode, () => res.element);
    restOfBarcode = res.rest;
  }

  return GS1Barcode(code: codeWithRest.code, elements: elements);
}