提问人:jdevp2 提问时间:11/17/2023 最后编辑:Ken Whitejdevp2 更新时间:11/17/2023 访问量:571
从 WillPopScope 更改为 PopScope 会引发 '!_debugLocked' 异常:不为 true
changing from WillPopScope to PopScope throws exception of '!_debugLocked': is not true
问:
我正在将我的代码从更改为捕获后退按钮的按下。在返回上一个屏幕之前,我需要显示一个对话框。因此,我需要发出两次,一次用于关闭对话框,另一次返回上一个屏幕。下面的代码工作正常,但是当我将其更改为“因为已弃用”时,会抛出错误。WillPopScope
PopScope
Navigator.pop
WillPopScope
'!_debugLocked': is not true
PopScope
WillPopScope
// return WillPopScope(
return PopScope(
canPop:true,
onPopInvoked: (bool didPop) async{
// onWillPop: () async {
if (vStatus[1] && !vStatus[2]) {
_showMaterialDialog(context, 1);
}
return Future.value(true);
},
child: Scaffold(
…….
void _showMaterialDialog(BuildContext context, int opt) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(getTextTitle(opt),style:TextStyle(fontFamily: 'DancingScriptBold',color:Colors.blue, fontSize: 20.0)),
content: Text(getTextContent(opt),
style: const TextStyle(
fontFamily: 'DancingScriptBold', color:Colors.blue,fontSize: 20.0),
),
actions: <Widget>[
TextButton(
onPressed: () {
Utils.saveVideo().then((value) async {
if(value!) {
vStatus[2] = true;
}
});
Navigator.of(context).pop(); //dismiss dialog
Navigator.of(context).pop(); //go back to previous screen
},
child: const Text(
style: TextStyle(
fontFamily: 'DancingScriptBold', color:Colors.blue,fontSize: 20.0),
'Yes')),
TextButton(
onPressed: () {
Navigator.of(context).pop(); //dismiss dialog
Navigator.of(context).pop(); //go back to previous screen
}
},
child: const Text(
style: TextStyle(
fontFamily: 'DancingScriptBold',color:Colors.blue, fontSize: 20.0),
'No'),
)
],
);
});
}
感谢任何反馈!
答:
3赞
Abhishek Karad
11/17/2023
#1
您可以使用以下代码
PopScope(
canPop: false,
onPopInvoked: (bool didPop) async {
if (didPop) return;
final NavigatorState navigator = Navigator.of(context);
if (vStatus[1] && !vStatus[2]) {
await _showMaterialDialog(context, 1);
}
navigator.pop();
},
),
评论
0赞
Nickolas de Luca Alberton
11/18/2023
您是否注意到该方法执行了两次?onPopInvoked
0赞
jdevp2
11/19/2023
它通过添加 Thanks 来工作。if(didPop) return;
0赞
Nickolas de Luca Alberton
11/19/2023
@jdevp2不,它没有,如果在 if 条件上添加断点,您可以看到该方法被调用了两次。一次为 = 和一次为 = 。我只是想知道这是否是有意的,因为以前没有这种行为。didPop
false
didPop
true
WillPopScope
评论