我试图从url中读取pdf,音频和打印包,但奇怪的是,我得到的不是一个,而是下面提到的三个错误。我还附上了图像,以清楚地看到我是在哪里得到的错误。
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;
}
}
}
发布于 2022-09-11 06:30:34
尝试这段代码
Future<Uint8List?> _fetchPdfContent(final String url) async {
var Response<List<int>> response
try {
response = await Dio().get<List<int>>(
url,
options: Options(responseType: ResponseType.bytes),
);
} catch (e) {
print(e);
throw "put your error";
}
return Uint8List.fromList(response.data);
}
https://stackoverflow.com/questions/73676921
复制相似问题