handleError method

Future<void> handleError({
  1. required BuildContext context,
  2. required String message,
  3. required AudioPlayer player,
  4. required List result,
  5. required Function setStateCallback,
  6. required ScannerLocalization localizations,
})

Implementation

Future<void> handleError({
  required BuildContext context,
  required String message,
  required AudioPlayer player,
  required List<dynamic> result,
  required Function setStateCallback,
  required ScannerLocalization localizations,
}) async {
  // Play the buzzer sound to indicate an error
  player.play(AssetSource(DigitScannerConstants().errorFilePath));

  // Check if the player has completed playing or if the result list is empty
  if (player.state == PlayerState.completed || result.isEmpty) {
    // Display a toast message with the provided error message
    Toast.showToast(
      context,
      type: ToastType.error,
      message: localizations.translate(message),
    );
  }

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

  // Update the state to allow processing again and indicate not busy
  setStateCallback();
}