Skip to content

Unable to check if the device has printing capabilities or not #102

@Barzi-Yassin

Description

@Barzi-Yassin

Flutter version:

macbookpro@macbookpros-MacBook-Pro myproject2-flutter-app % flutter --version
Flutter 3.35.6 • channel stable • https://github.com/flutter/flutter.git
Tools • Dart 3.9.2 • DevTools 2.48.0

pubspec.yaml

dependencies:
  sunmi_printer_plus: ^4.1.1

Tried:

await SunmiPrinter.initPrinter(); // deprecated
// and
final bool printerAvailable = await SunmiPrinter.bindingPrinter(); // deprecated, 
// both deprecated

i have this solution working, but want to find a way without using deprecated methods?

Future<void> printListImages(List<Uint8List> listImages) async {

  if (listImages.isEmpty) {
    Notify.showWarn(
      title: 'Printing Status',
      message: 'No receipt found to print.',
    );
    return;
  }

  try {
    // Try initializing printer — will fail gracefully if not a Sunmi device
    await SunmiPrinter.initPrinter();

    // If initialization succeeds, print images
    for (Uint8List image in listImages) {
      await SunmiPrinter.printImage(
        image,
        align: SunmiPrintAlign.CENTER,
      );
    }
    await SunmiPrinter.cutPaper();
  } on PlatformException catch (e) {
    // Handle non-Sunmi devices or uninitialized printer service
    if (e.message?.contains('sunmiPrinter has not been initialized') ?? false) {
      Notify.showWarn(
        title: 'Printing Status',
        message: 'No Sunmi printer detected on this device.',
      );
    } else {
      Notify.showError(
        title: 'Printing Error',
        message: 'Unable to print: ${e.message}',
      );
    }
  } catch (e) {
    // Catch-all for other unexpected errors
    Notify.showError(
      title: 'Printing Error',
      message: 'Unexpected error: $e',
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions