我想在我的Flutter应用中显示PDF文件。因此,我必须下载它。用户界面应等待pdf文件生成后再显示内容。这已经可以了。
但是下载文件后,“ future”功能将被无限调用,并且一次又一次地加载pdf。
这是代码:
class PDFView extends StatefulWidget {
String urlPDF = "";
String namePDF = "";
PDFView(this.urlPDF, this.namePDF);
@override
_PDFView createState() => new _PDFView(urlPDF, namePDF);
}
class _PDFView extends State<PDFView> {
String urlPDF = "";
String namePDF = "";
String pathPDF ="";
_PDFView(this.urlPDF, this.namePDF);
Future<File> _future;
@override
void initState() {
super.initState();
_future = createFileOfPdfUrl(urlWebsite + urlPDF); //returns a Future<File>
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<File>(
future: _future.then((result) {setState(() {pathPDF = result.path; print(pathPDF);});}),
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
if (pathPDF == "")
{
return Form(
child: Center(
child: CircularProgressIndicator()
),
);
}
if (pathPDF != "")
{
return PDFViewerScaffold(
appBar: new ViewerAppBar(title: namePDF),
path: pathPDF);
}
},
)
);
}
}