skip to Main Content

After updating Flutter to version 3.22.0 I have a problem with pop using to exit from APP.

Before the update I used PopScope before the Scaffold widget and the callback onPopInvoked provided worked correctly.
Using a bool I knew if it was the first time that I pressed the turn back button or the second time, and I was able to exit the app correctly.

After the Flutter update it seems that the callback was not triggered.

Have someone the same problem?

2

Answers


  1. WillPopScope is deprecated, ref https://docs.flutter.dev/release/breaking-changes/android-predictive-back

    You can try with my quick fix:

    int _qtyPressBack = 0;
    
    @override
    Widget build(BuildContext context) {
      return PopScope(
        canPop: _qtyPressBack >= 1,
        onPopInvoked: (didPop) {
          setState(() {
            _qtyPressBack++;
          });
          if (didPop) {
            return;
          }
        },
        child: Scaffold(
          appBar: AppBar(
            title: const Text("Test"),
            backgroundColor: Colors.grey,
          ),
          body: const Placeholder(),
        ),
      );
    }
    

    enter image description here

    Login or Signup to reply.
  2. I had a similar issue with the PopScope after updating to 3.22.0. I am pretty sure Flutter changed how the PopScope’s onPopInvoked works between 3.19.6 & 3.22.0

    Here is how it works from 3.22.0 onwards:

    canPop still determines if the page is popped when the user tries to pop with a Gesture (or Android back-button).

    onPopInvoked triggeres regardless who tries to pop (user or developer). If the developer called pop (e.g. Navigator.pop(context)) the bool within onPopInvoked (called didPop) will be true. If the user did it, didPop will be false. Here is the problem: If you set canPop to false & the user tries to pop your code with in onPopInvoked starts to run (so far do good). But if you have custom navigation within onPopInvoked that involves a manual pop (from you, the dev) the onPopInvoked will be run a 2nd time (This is what I think changed). The 2nd time the bool didPop is true. So your custom navigation will also run 2 times.

    How to stop onPopInvoked from running twice:

    canPop: true,
    onPopInvoked: (didPop){
        if (didPop) {
          return;
        }
        // you custom navigation with a pop here
      },
    

    Hope this helps someone 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search