0
votes

I want to print the screenshot image of first page in Flutter. So far I have come up to the part where I need to parse that image into function that would print it, here is the code:

FIRST PAGE

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

So this is the page that screenshots that whole page and pushes that screenshot to the second screen. Here is the second screen:

SECOND PAGE

 class Print extends StatefulWidget {

  final File screenshot;

  Print(this.screenshot);

  @override
  _PrintState createState() => _PrintState();
}

Here I initialize that image from the first page, and then I created the function for printing that should print the ticket, but I am getting an error:

Future<Ticket> _ticket(PaperSize paper) async {
final ticket = Ticket(paper);


//this is a working option, but I don't want to load from assets, but load screenshot file from first page
//final ByteData data = await rootBundle.load(assets/store.png');

final File img = await rootBundle.load(widget.screenshot); //here parse the image from first page **ERROR HERE**
final Uint8List bytes = data.buffer.asUint8List();
final Image image = decodeImage(bytes);
ticket.image(image);

return ticket;
}

I am getting an error that Byte data cannot be assigned to variable type File which I understand, but how would I go around making this work? I am using the esc_pos_bluetooth: ^0.2.8 library for bluetooth printing.

1

1 Answers

0
votes

You don't need to load data from rootBundle instead, you can use readAsBytes() class method from File class which returns Uint8List.

Uint8List bytes = await widget.screenshot.readAsBytes();

Your final code should look like.

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

  return ticket;
}