0
votes

I have this function that should print a receipt with bluetooth printer. It should print some text and some files (screenshot Image and logo File). I am getting an error for some reason, here it is:

[ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method 'readAsBytes' was called on null.
E/flutter ( 2806): Receiver: null
E/flutter ( 2806): Tried calling: readAsBytes()

It says that readAsBytes was called on null, but I don't think it is, check the code below:

  class Print extends StatefulWidget {
 
  final File screenshot;
  Print({this.screenshot});

Future<Ticket> _ticket(PaperSize paper) async {
    final ticket = Ticket(paper);
    final Uint8List bytes = await widget.screenshot.readAsBytes();
    final Image image = decodeImage(bytes);
    ticket.image(image);}

That is the print screen, and the function that sends that screenshot to this screen is this one:

_screenshot() {
screenshotController.capture(pixelRatio: 1.5).then((File image) {
  //Capture Done
  setState(() {
    _screenshot = image;
  });
  print('Successful Screenshot => $_screenshot');
  Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) => Print(
                screenshot: _screenshot,
                
              )));
  print(_screenshot.path);
  //print('$_screenshot deleted');
}).catchError((onError) {
  print(onError);
});

}

I am using the esc_pos_bluetooth: ^0.2.8 library. Let me know if you guys know what this error is and how it could be solved and let me know if you have any more questions.

1

1 Answers

0
votes

Setstate is trying to rebuild the widget. Try without using setstate.

Instead, try this:

_screenshot() {
screenshotController.capture(pixelRatio: 1.5).then((File image) {
  //Capture Done

    _screenshot = image;

  print('Successful Screenshot => $_screenshot');
  Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) => Print(
                screenshot: _screenshot,
                
              )));
  print(_screenshot.path);
  //print('$_screenshot deleted');
}).catchError((onError) {
  print(onError);
});