Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

dart - Null Error with Flutter Bluetooth Printer

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.

question from:https://stackoverflow.com/questions/65905506/null-error-with-flutter-bluetooth-printer

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...