storeCode method

Future<void> storeCode({
  1. required BuildContext context,
  2. required String code,
  3. required AudioPlayer player,
  4. required bool singleValue,
  5. required dynamic updateCodes(
    1. List<String>
    ),
  6. required List<String> initialCodes,
})

Implementation

Future<void> storeCode({
  required BuildContext context,
  required String code,
  required AudioPlayer player,
  required bool singleValue,
  required Function(List<String>) updateCodes,
  required List<String> initialCodes,
}) async {
  // Play the add sound to indicate a successful scan
  player.play(AssetSource(DigitScannerConstants().audioFilePath));

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

  // Make a copy of the current QR codes from the bloc state
  List<String> codes = List.from(initialCodes);

  // If the widget is supposed to handle a single value, clear the codes list
  if (singleValue) {
    codes = [];
  }

  // Add the new code to the list
  codes.add(code);

  // Update the state with the new list of codes
  updateCodes(codes);

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

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