init method

  1. @override
Future<void> init({
  1. required String macAddress,
  2. OnPrinterConnectionChange? onPrinterConnectionChange,
  3. OnPrinterOperationChange? onDocPrinted,
})
override

Initializes the Rongta printer with the provided parameters.

The macAddress parameter specifies the MAC address of the printer. The onPrinterConnectionChange parameter (optional) is a callback function that will be invoked when the printer connection status changes. The onDocPrinted parameter (optional) is a callback function that will be invoked when a document is successfully printed.

Returns a Future that completes when the initialization is finished.

Implementation

@override
Future<void> init({
  required String macAddress,
  OnPrinterConnectionChange? onPrinterConnectionChange,
  OnPrinterOperationChange? onDocPrinted,
}) async {
  // Set the method call handler to handle platform method calls.
  methodChannel.setMethodCallHandler((call) {
    switch (call.method) {
      case 'on_printer_connected':
        if (onPrinterConnectionChange != null) {
          onPrinterConnectionChange(PrinterConnectionStatus.connected);
        }
        break;
      case 'on_printer_disconnected':
        if (onPrinterConnectionChange != null) {
          onPrinterConnectionChange(PrinterConnectionStatus.disconnected);
        }
        break;
      case 'on_printer_write_completion':
        if (onDocPrinted != null) {
          onDocPrinted(PrinterOperationStatus.idle);
        }
        break;
    }
    return Future.value();
  });

  // Invoke the platform method 'init' with the provided parameters.
  return await methodChannel.invokeMethod(
    'init',
    {
      'mac_address': macAddress,
    },
  );
}