如何处理或覆盖 flutter 应用程序中 android 设备后退按钮的操作?

How to handle or override the action of android device back button in flutter application?

我想将用户导航到主屏幕并在按下另一个屏幕上的某个按钮后删除之前的导航堆栈。目前我使用 Navigator.pushNamedAndRemoveUntil 方法。但情况是当我在主屏幕上按下设备后退按钮时我的应用程序崩溃所以我想处理或覆盖设备后退按钮操作。

像这样使用 WillPopScope 小部件

return new WillPopScope(
    onWillPop: () async {
   //  your logic here 
   //  
   return Future.value(false); // true if you want the back button to take effect 
},
    child: new Scaffold(
     //then the rest of your code...