From 797056127b0c20d5eea54549bf49432d89ac5622 Mon Sep 17 00:00:00 2001 From: Rafa Ruiz Date: Mon, 9 Feb 2026 18:38:13 +0100 Subject: [PATCH] Add feature ignore unknown AIs --- lib/src/barcode_parser.dart | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/src/barcode_parser.dart b/lib/src/barcode_parser.dart index d31214e..a14942c 100644 --- a/lib/src/barcode_parser.dart +++ b/lib/src/barcode_parser.dart @@ -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'); } @@ -88,7 +88,19 @@ class GS1BarcodeParser { final elements = {}; 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; } @@ -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 customAIs = const {}]) { if (data.isEmpty) { throw GS1ParseException(message: 'AI not found for $data. AI is empty'); @@ -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'); }