Use rootBundle.load()
(await rootBundle.load(/*YOUR IMAGE PATH HERE*/)).buffer.asUint8List()
UPDATE
As load()
is an async operation, you need to wait until the data is fully loaded. Try substituting the UI with some loading indicator until then.
ByteData imageData;
@override
void initState() {
rootBundle.load('assets/test.jpg')
.then((data) => setState(() => this.imageData = data));
}
@override
Widget build(BuildContext context) {
if (imageData == null) {
return Center(child: CircularProgressIndicator());
}
final image = PdfImage(
pdf.document,
image: imageData.buffer.asUint8List(),
width: img.width,
height: img.height,
);
...
}
rootBundle.load()
, as it returns aFuture
you have to usethen
method afterload
completes – Deckhandasync
? i said: usethen
method – DeckhandPdfImage
? what do you need it for? what does it have to do with your customStatelessWidget
? is it used by itsbuild()
method? - if you just want to save your pdf file then do not mix pdf widgets and flutter widgets – Deckhand