Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/src/barcode_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class GS1BarcodeParser {
}

/// Parse barcode string
GS1Barcode parse(String data, {CodeType? codeType}) {
GS1Barcode parse(String data, {CodeType? codeType, bool ignoreUnknownAIs = false}) {
if (data.isEmpty) {
GS1DataException(message: 'Barcode is empty');
}
Expand All @@ -88,7 +88,19 @@ class GS1BarcodeParser {
final elements = <String, GS1ParsedElement>{};

while (restOfBarcode.isNotEmpty) {
final res = _identifyAI(restOfBarcode, _config.customAIs);
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;
}
Expand All @@ -104,7 +116,7 @@ class GS1BarcodeParser {
customAIs[ai] ?? AI.AIS[ai];

/// Get ans parse AI
ParsedElementWithRest _identifyAI(String data,
ParsedElementWithRest? _identifyAI(String data, bool ignoreUnknownAIs,
[Map<String, AI> customAIs = const {}]) {
if (data.isEmpty) {
throw GS1ParseException(message: 'AI not found for $data. AI is empty');
Expand All @@ -127,6 +139,10 @@ class GS1BarcodeParser {
ai = _getAI(fourNumber, customAIs);
}
if (ai == null) {
if (ignoreUnknownAIs) {
return null;
}

throw GS1ParseException(message: 'AI not found for $data');
}

Expand Down