esc_pos_printer
esc_pos_printer copied to clipboard
Socket has not been initialized when calling rawBytes method
I got this error sometimes when calling printer.rawBytes(buffer). Looking at the source code, I don't understand how socket has not been initialized if connect result is success.
Package code :
Future<PosPrintResult> connect(String host,
{int port = 91000, Duration timeout = const Duration(seconds: 5)}) async {
_host = host;
_port = port;
try {
_socket = await Socket.connect(host, port, timeout: timeout);
_socket.add(_generator.reset());
return Future<PosPrintResult>.value(PosPrintResult.success);
} catch (e) {
return Future<PosPrintResult>.value(PosPrintResult.timeout);
}
}
My code :
final PosPrintResult result = await _networkPrinter.connect(ipAddress, port: 9100);
if (result == PosPrintResult.success) {
_networkPrinter.rawBytes(_buffer);
_networkPrinter.cut();
_networkPrinter.disconnect();
}