parse method

  1. @override
ExpandedProductParsedResult? parse(
  1. Result result
)
override

Attempts to parse the raw Result's contents as a particular type of information (email, URL, etc.) and return a ParsedResult encapsulating the result of parsing.

@param theResult the raw Result to parse @return ParsedResult encapsulating the parsing result

Implementation

@override
ExpandedProductParsedResult? parse(Result result) {
  final format = result.barcodeFormat;
  if (format != BarcodeFormat.rssExpanded) {
    // ExtendedProductParsedResult NOT created. Not a RSS Expanded barcode
    return null;
  }
  final rawText = ResultParser.getMassagedText(result);

  String? productID;
  String? sscc;
  String? lotNumber;
  String? productionDate;
  String? packagingDate;
  String? bestBeforeDate;
  String? expirationDate;
  String? weight;
  String? weightType;
  String? weightIncrement;
  String? price;
  String? priceIncrement;
  String? priceCurrency;
  final uncommonAIs = <String, String>{};

  int i = 0;

  while (i < rawText.length) {
    final ai = _findAIValue(i, rawText);
    if (ai == null) {
      // Error. Code doesn't match with RSS expanded pattern
      // ExtendedProductParsedResult NOT created. Not match with RSS Expanded pattern
      return null;
    }
    i += ai.length + 2;
    final value = _findValue(i, rawText);
    i += value.length;

    switch (ai) {
      case '00':
        sscc = value;
        break;
      case '01':
        productID = value;
        break;
      case '10':
        lotNumber = value;
        break;
      case '11':
        productionDate = value;
        break;
      case '13':
        packagingDate = value;
        break;
      case '15':
        bestBeforeDate = value;
        break;
      case '17':
        expirationDate = value;
        break;
      case '3100':
      case '3101':
      case '3102':
      case '3103':
      case '3104':
      case '3105':
      case '3106':
      case '3107':
      case '3108':
      case '3109':
        weight = value;
        weightType = ExpandedProductParsedResult.kilogram;
        weightIncrement = ai.substring(3);
        break;
      case '3200':
      case '3201':
      case '3202':
      case '3203':
      case '3204':
      case '3205':
      case '3206':
      case '3207':
      case '3208':
      case '3209':
        weight = value;
        weightType = ExpandedProductParsedResult.pound;
        weightIncrement = ai.substring(3);
        break;
      case '3920':
      case '3921':
      case '3922':
      case '3923':
        price = value;
        priceIncrement = ai.substring(3);
        break;
      case '3930':
      case '3931':
      case '3932':
      case '3933':
        if (value.length < 4) {
          // The value must have more of 3 symbols (3 for currency and
          // 1 at least for the price)
          // ExtendedProductParsedResult NOT created. Not match with RSS Expanded pattern
          return null;
        }
        price = value.substring(3);
        priceCurrency = value.substring(0, 3);
        priceIncrement = ai.substring(3);
        break;
      default:
        // No match with common AIs
        uncommonAIs[ai] = value;
        break;
    }
  }

  return ExpandedProductParsedResult(
    rawText,
    productID,
    sscc,
    lotNumber,
    productionDate,
    packagingDate,
    bestBeforeDate,
    expirationDate,
    weight,
    weightType,
    weightIncrement,
    price,
    priceIncrement,
    priceCurrency,
    uncommonAIs,
  );
}