Flutter:使用FutureBuilder无休止地重建页面

时间:2019-12-15 13:17:22

标签: flutter future pdf-viewer

我想在我的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);
          }
          },
        )
    );
  }
}

0 个答案:

没有答案