print method
Sends a list of print instructions to the native platform via the method channel.
The items parameter contains a list of ItemPrintModel objects representing
the instructions for the POS device.
Example:
final items = [
ItemPrintModel.text(
content: "TEXTO CENTRALIZADO",
align: Align.center,
fontFormat: FontFormat.medium,
),
];
final result = await methodChannelGetnetPos.print(items);
print(result); // "Printed successfully"
items - A list of ItemPrintModel objects describing the print instructions.
Returns:
- A
Future<String?>that resolves to a success message or null if the operation fails.
Throws:
- Any error encountered during the printing process.
Implementation
@override
Future<String?> print(List<ItemPrintModel> items) async {
try {
if (printInProgress) {
return null; // Prevent concurrent print operations
}
if (items.isEmpty) {
return null; // No instructions provided
}
printInProgress = true;
// Convert the items to a list of maps for platform communication
final itemsMaps =
items.map((instruction) => instruction.toMap()).toList();
// Invoke the native method
final result =
await methodChannel.invokeMethod<String?>('print', itemsMaps);
printInProgress = false;
return result;
} catch (e) {
printInProgress = false;
rethrow;
}
}