I am trying to read the pdf from url using pdf, dio and printing packages but the weird thing is I am getting not one but three errors mentioned below. I have also attached the image for clear view where I am getting the errors.
The argument type 'Future<Uint8List?>' can't be assigned to the parameter type 'Future<Uint8List>?
The return type 'Uint8List?' isn't a 'FutureOr<Uint8List>', as required by the closure's context.
The argument type 'List<int>?' can't be assigned to the parameter type 'List<int>'.
class _MyHomePageState extends State<MyHomePage> {
String _url =
"https://www.au-sonpo.co.jp/corporate/upload/article/89/article_89_1.pdf";
void _refresh() {
setState(() {
// other _url
_url =
"https://www.au-sonpo.co.jp/corporate/upload/article/90/article_90_1.pdf";
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: FutureBuilder<Uint8List>(
future: _fetchPdfContent(_url),
builder: (context, snapshot) {
if (snapshot.hasData) {
return PdfPreview(
allowPrinting: false,
allowSharing: false,
canChangePageFormat: false,
initialPageFormat:
PdfPageFormat(100 * PdfPageFormat.mm, 120 * PdfPageFormat.mm),
build: (format) => snapshot.data,
);
}
return Center(
child: CircularProgressIndicator(),
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: _refresh,
tooltip: 'Refesh',
child: Icon(Icons.refresh),
),
);
}
Future<Uint8List?> _fetchPdfContent(final String url) async {
try {
final Response<List<int>> response = await Dio().get<List<int>>(
url,
options: Options(responseType: ResponseType.bytes),
);
return Uint8List.fromList(response.data);
} catch (e) {
print(e);
return null;
}
}
}
2
Answers
In your
FutureBuilder
, you expectUint8List
but in your future you are return nullable type, Try this:try this code