parseGS1Barcode method

GS1Barcode parseGS1Barcode(
  1. String barcode
)

Implementation

GS1Barcode parseGS1Barcode(String barcode) {
  if (barcode.startsWith(']C1')) {}

  final String delimiter = barcode.contains(String.fromCharCode(29))
      ? String.fromCharCode(29)
      : '!';
  // Split the data into individual AIs using the FNC1 character
  final List<String> elements = barcode.split(delimiter)
    ..removeWhere((element) => element.isEmpty);

  // Create a map to store the parsed AI data
  final Map<String, GS1ParsedElement> aiData = {};

  for (final String element in elements) {
    final List<(String, String)>? aiValues = _aiValuesOf(element);

    if (aiValues == null) throw FormatException(barcode);

    for (final (aiCode, aiValue) in aiValues) {
      // Store the AI code and value in the map
      aiData[aiCode] = GS1ParsedElement(
        aiCode: aiCode,
        rawData: aiValue,
        data: aiValue,
      );
    }
  }

  if (aiData.isEmpty) throw FormatException(barcode);

  return GS1Barcode(
    code: Code(
      type: CodeType.GS1_128,
      codeTitle: 'Product code',
      fnc1: delimiter,
    ),
    elements: aiData,
  );
}