【问题标题】:Flutter: 'Future<PDFDocument>' is not a subtype of type 'Widget'Flutter:“Future<PDFDocument>”不是“Widget”类型的子类型
【发布时间】:2020-01-18 14:55:53
【问题描述】:

我正在尝试创建一个页面,该页面在打开页面时显示来自资产的 PDF。 PDF 加载,但在每次加载之前都会在标题中引发错误。我很确定这与函数的调用方式有关,但我不知道如何解决。

我的代码示例:

class Calendar2020PDFScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _Calendar2020PDFScreenState();
}

class _Calendar2020PDFScreenState extends State<Calendar2020PDFScreen> {
  bool _isLoading = false, _isInit = true;
  PDFDocument document;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.blue,
          leading: new IconButton(
            icon: new Icon(Icons.arrow_back, color: Colors.black),
            onPressed: () => Navigator.of(context).pop(),
          ),
          title: Center(
              child: Text(
            'MCHD',
            style: TextStyle(color: Colors.white),
          )),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.search),
              onPressed: () {
                showSearch(context: context, delegate: DataSearch());
              },
            ) //IconButton
          ], //,Widget>[]
        ), //Appbar
        body: Column(
          children: <Widget>[
            Expanded(
              child: Center(
                child: _isInit
                    ? loadFromAssets() // Executes loadFromAssets function if _isInit is true
                    : _isLoading
                        ? Center(
                            child:
                                CircularProgressIndicator(), //Shows indicator if _isLoading is true
                          ) //Center
                        : PDFViewer(
                            document: document,
                          ), //PDFViewer
              ), //Center
            ), //Expanded
          ], //<Widget>[]
        ), //Column
      ), //Scaffold
    ); //MaterialApp
  }

  Future<PDFDocument> loadFromAssets() async {
    try {
      setState(() {
        _isInit = false; //remove text
        _isLoading = true; //show loading
      });
      document = await PDFDocument.fromAsset("assets/PDFs/calendars_2020.pdf");
      setState(() {
        _isLoading = false; //remove loading
      });
      return document;
    } catch (err) {
      print('Caught error: $err');
    } //catch
  } //Future
}

【问题讨论】:

标签: flutter dart


【解决方案1】:

class Calendar2020PDFScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _Calendar2020PDFScreenState();
}

class _Calendar2020PDFScreenState extends State<Calendar2020PDFScreen> {
  bool _isLoading = false, _isInit = true;
  PDFDocument document;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadFromAssets();
    // this is where you load your asssets
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.blue,
          leading: new IconButton(
            icon: new Icon(Icons.arrow_back, color: Colors.black),
            onPressed: () => Navigator.of(context).pop(),
          ),
          title: Center(
              child: Text(
            'MCHD',
            style: TextStyle(color: Colors.white),
          )),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.search),
              onPressed: () {
                showSearch(context: context, delegate: DataSearch());
              },
            ) //IconButton
          ], //,Widget>[]
        ), //Appbar
        body: Column(
          children: <Widget>[
            Expanded(
              child: Center(
                child: _isLoading
                        ? Center(
                            child:
                                CircularProgressIndicator(), //Shows indicator if _isLoading is true
                          ) //Center
                        : PDFViewer(
                            document: document,
                          ), //PDFViewer
              ), //Center
            ), //Expanded
          ], //<Widget>[]
        ), //Column
      ), //Scaffold
    ); //MaterialApp
  }

  Future<PDFDocument> loadFromAssets() async {
    try {
      setState(() {
        _isLoading = true; //show loading
      });
      document = await PDFDocument.fromAsset("assets/PDFs/calendars_2020.pdf");
      setState(() {
        _isLoading = false; //remove loading
      });
      return document;
    } catch (err) {
      print('Caught error: $err');
    } //catch
  } //Future
}

也许你应该看看这个,你首先在 initState 中加载你的资产,而这个方法加载循环进度,加载后它会给你想要的输出,没有尝试但安排了它。 试一试让我知道是否有任何问题。 否则,如果同时发生变化,您可以使用 future builder。

只需检查文档的 null 条件即可。

谢谢。

【讨论】:

    猜你喜欢
    • 2020-04-07
    • 2019-08-18
    • 1970-01-01
    • 2021-02-17
    • 2020-09-11
    • 2023-03-30
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多