John Smith
John Smith

Reputation: 259

How to execute a function when the device's physical back button is pressed in flutter?

Is there a way to handle the android device's physical back button, what I mean is that I'm not using an AppBar in the application so there's no back button the only back button there is the device physical back button and I want to handle the click on it.

this is the button I'm talking about

enter image description here

how can I do that??

Upvotes: 0

Views: 635

Answers (2)

Muhammad Rameez
Muhammad Rameez

Reputation: 81

But what if, I want to perform another function or call my function when this back button is pressed.

I am not able to call it there in WillPopScope because it only takes boolean value.

For Example :

return WillPopScope(
  onWillPop: () async {
    Navigator.of(context).pop(true);
  },

I am getting this kind of error

The body might complete normally, causing 'null' to be returned, but the return type, 'FutureOr', is a potentially non-nullable type.

Upvotes: 0

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27137

You can use WillPopScope widget to do so.

following code may help you more.

WillPopScope(
    onWillPop: () async => false,
    child: Scaffold(
      appBar: AppBar(
        title: Text("Second Screen"),
      ),

Upvotes: 1

Related Questions