从 WillPopScope 更改为 PopScope 会引发 '!_debugLocked' 异常:不是 true

jde*_*vp2 10 navigator flutter

我正在将代码从 更改为WillPopScopePopScope捕获后退按钮的按下情况。我需要在返回上一屏幕之前显示一个对话框。因此,我需要发出Navigator.pop两次,一次关闭对话框,另一次返回上一个屏幕。下面的代码可以正常工作,但是当我将其更改为“since is depreciated”时,会抛出WillPopScope错误 。'!_debugLocked': is not truePopScopeWillPopScope

\n
// return WillPopScope(\nreturn PopScope(\n    canPop:true,\n    onPopInvoked: (bool didPop) async{\n  //  onWillPop: () async {\n    if (vStatus[1] && !vStatus[2]) {\n      _showMaterialDialog(context, 1);\n    }\n    return Future.value(true);\n  },\n  child: Scaffold(\n   \xe2\x80\xa6\xe2\x80\xa6.\n\n\nvoid _showMaterialDialog(BuildContext context, int opt) {\n  showDialog(\n      context: context,\n      builder: (context) {\n        return AlertDialog(\n          title:  Text(getTextTitle(opt),style:TextStyle(fontFamily: 'DancingScriptBold',color:Colors.blue, fontSize: 20.0)),\n          content: Text(getTextContent(opt),\n              style: const TextStyle(\n                  fontFamily: 'DancingScriptBold', color:Colors.blue,fontSize: 20.0),\n              ),\n          actions: <Widget>[\n            TextButton(\n                onPressed: ()  {\n                    Utils.saveVideo().then((value) async {\n                     if(value!) {\n                        vStatus[2] = true;\n                    }\n                  });\n             \n                    Navigator.of(context).pop(); //dismiss dialog\n                    Navigator.of(context).pop(); //go back to previous screen             \n            \n                },\n                child: const Text(\n                    style: TextStyle(\n                        fontFamily: 'DancingScriptBold', color:Colors.blue,fontSize: 20.0),\n                    'Yes')),\n            TextButton(\n              onPressed: () {\n              \n                 Navigator.of(context).pop(); //dismiss dialog\n                 Navigator.of(context).pop(); //go back to previous screen  \n               \n                }\n              },\n              child: const Text(\n                  style: TextStyle(\n                      fontFamily: 'DancingScriptBold',color:Colors.blue, fontSize: 20.0),\n                  'No'),\n            )\n          ],\n        );\n      });\n}\n
Run Code Online (Sandbox Code Playgroud)\n

感谢任何反馈!

\n

小智 11

根据发行说明https://docs.flutter.dev/release/writing-changes/android-predictive-back#migration-a-back-confirmation-dialog:~:text=route%20transitions.%0A%7D-,正在迁移%20a%20back%20confirmation%20dialog,-WillPopScope%20was%20sometimes

您可以使用下面的代码

  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();
  },
),
Run Code Online (Sandbox Code Playgroud)