storeValue method

Future<void> storeValue({
  1. required BuildContext context,
  2. required GS1Barcode scanData,
  3. required AudioPlayer player,
  4. required dynamic updateResult(
    1. List<GS1Barcode>
    ),
  5. required List<GS1Barcode> initialResult,
})

Implementation

Future<void> storeValue({
  required BuildContext context,
  required GS1Barcode scanData,
  required AudioPlayer player,
  required Function(List<GS1Barcode>) updateResult,
  required List<GS1Barcode> initialResult,
}) async {
  // Assign the scanned data to a local variable
  final parsedResult = scanData;

  // Access the DigitScannerBloc from the context
  final bloc = context.read<DigitScannerBloc>();

  // Play the add sound to indicate a successful scan
  player.play(AssetSource(DigitScannerConstants().audioFilePath));

  // Wait for 3 seconds before proceeding
  await Future.delayed(const Duration(seconds: 3));

  // Make a copy of the current barcodes from the bloc state
  List<GS1Barcode> result = List.from(initialResult);

  // Remove duplicate entries based on the last value in the elements map
  result.removeDuplicates(
    (element) => element.elements.entries.last.value.data,
  );

  // Add the new parsed result to the list
  result.add(parsedResult);

  // Dispatch an event to update the bloc with the new barcode and existing QR code lists
  bloc.add(DigitScannerEvent.handleScanner(
    barCode: result, // Update barcodes with the new list
    qrCode: bloc.state.qrCodes, // Keep existing QR codes
  ));

  // Update the state with the new list of results
  updateResult(result);

  // Wait for 5 seconds before completing the function
  await Future.delayed(const Duration(seconds: 5));
}