设备后退按钮在抖动中不起作用

Jag*_*esh 6 dart flutter

在我的应用程序中,当我单击设备的后退按钮时,它正在关闭应用程序。我使用了 Willpopscope 但它不起作用。但是当我创建一个示例项目时它正在工作。任何人都可以解释一下为什么它在现有应用程序中不起作用。我分享了我的代码。(在应用栏中后退箭头工作正常,但问题在于设备后退按钮)

    Future<bool> _onWillPop() {
    return showDialog(
          context: context,
          builder: (context) => new AlertDialog(
                title: new Text('Are you sure?'),
                content: new Text('Unsaved data will be lost.'),
                actions: <Widget>[
                  new FlatButton(
                    onPressed: () => Navigator.of(context).pop(false),
                    child: new Text('No'),
                  ),
                  new FlatButton(
                    onPressed: () => Navigator.of(context).pop(true),
                    child: new Text('Yes'),
                  ),
                ],
              ),
        ) ??
        false;
  }

@override
  Widget build(BuildContext context) {
    return new WillPopScope(
      onWillPop: _onWillPop,
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text(
            "On Back pressed", 
            style: new TextStyle(color: Colors.white),
          ),
        ),
        body: new Center(
          child: new Text("Home Page"),
        ),
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

Mur*_*lan 1

_onWillPop用这个替换你的方法_onWillPop()async{ return false; }