我有这个功能,应该用蓝牙打印机打印收据。它应该打印一些文本和一些文件(截图图像和logo文件)。由于某种原因,我得到了一个错误,它是:
[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()
它说readAsBytes在null时被调用,但我不认为它是,检查下面的代码:
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);}
这是打印屏幕,将屏幕截图发送到屏幕的函数是这样的:
_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);
});
}
我使用esc_pos_bluetooth: ^0.2.8库。如果你们知道这个错误是什么,以及如何解决它,请告诉我,如果你们有任何问题,请告诉我。
Setstate正在尝试重建小部件。
不如试试这样:
_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);
});