andrey-ushakov / esc_pos_printer

ESC/POS (thermal, receipt) printing for Flutter & Dart
BSD 3-Clause "New" or "Revised" License
346 stars 298 forks source link

Socket has not been initialized when calling rawBytes method #172

Open nerahmou opened 1 year ago

nerahmou commented 1 year ago

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();
    }