请帮助我解决颤振应用程序中的此错误:
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(),
),
));
}
}
Run Code Online (Sandbox Code Playgroud)
Future<bool>_onBackPressed(BuildContext context) async{ 这个错误出现在错误框中错误:
the argument type Future<bool>Function(BuildContext) can not be assigned to parameter Future<bool>Function()
Run Code Online (Sandbox Code Playgroud)
如果您知道添加ON DOUBLE PRESS EXIT FLUTTER APP 的更好方法,请提及
好吧,onWillPop参数需要 a Future<bool> Function(),但是您提供了一个需要BuildContext作为参数的函数(因此类型为Future<bool> Function(BuildContext context))。
正确的方法是将您的函数包装在另一个函数中:
WillPopScope(
onWillPop: () => _onBackPressed(context),
child: ...
)
Run Code Online (Sandbox Code Playgroud)
而且我认为没有比保存上次回按的时间戳并将其与按回按的当前时间进行比较更优雅的方法来检测两次回按。
| 归档时间: |
|
| 查看次数: |
1666 次 |
| 最近记录: |