请帮助我解决我的flutter应用程序中的此错误:
class MyApp extends StatelessWidget {
Future<bool> _onBackPressed(BuildContext context) {
return showDialog(
context:context,builder:(BuildContext context)=>AlertDialog(title: Text('Exit'),
actions: <Widget>[
FlatButton( child:Text('NO'),onPressed: ()=>Navigator.pop(context,false)),
FlatButton( child:Text('Yes'),onPressed: ()=>Navigator.pop(context,true))
],));
}
@override
Widget build(BuildContext context) {
return WillPopScope(onWillPop:_onBackPressed, // **the argument type Future<bool>Function(BuildContext) can not be assigned to parameter Future<bool>Function()**
child: MaterialApp(
debugShowCheckedModeBanner: false,
title: 'ListViews',
theme: ThemeData(
primarySwatch: Colors.teal,
),
home: Scaffold(
appBar: AppBar(title: Text('ListViews')),
body: BodyLayout(),
),
));
}
}
Future<bool>_onBackPressed(BuildContext context) async{
后,此错误出现在错误框中错误:
the argument type Future<bool>Function(BuildContext) can not be assigned to parameter Future<bool>Function()
如果您有更好的方法可以添加,请在DOUBLE PRESS EXIT FLUTTER APP 中添加
最佳答案
好吧,onWillPop
参数需要一个Future<bool> Function()
,但是您提供了一个需要BuildContext
作为参数的函数(因此是Future<bool> Function(BuildContext context)
类型)。
正确的方法是将函数包装在另一个函数中:
WillPopScope(
onWillPop: () => _onBackPressed(context),
child: ...
)
而且,我认为没有比检测上次回按的时间戳并将其与按回去的当前时间进行比较的更为优雅的方法来检测两次回按。
关于flutter - onWillPop-无法将Future <bool> Function(BuildContext)分配给参数类型Future <bool> Function(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58100060/