![](/img/trans.png)
[英]Flutter: type 'Future<dynamic>' is not a subtype of type 'Widget'
[英]Flutter: 'Future<PDFDocument>' is not a subtype of type 'Widget'
我正在嘗試制作一個頁面,在打開頁面時顯示來自資產的 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
}
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 中加載你的資產,而這個方法加載循環進度,加載后它會給你所需的輸出,沒有嘗試而是安排它。 試一試讓我知道是否有任何問題。 否則,如果同時發生變化,您可以使用未來的構建器。
只需檢查文檔的空條件。
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.