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) {
    // The AI code is the first two characters
    final String? aiCode = _validItemAiList.firstWhereOrNull(
      element.startsWith,
    );

    if (aiCode == null) {
      throw FormatException(barcode);
    }

    // The AI value is everything after the first two characters
    final String aiValue = element.substring(aiCode.length);

    // 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,
  );
}